File: test_singletoninstance.py

package info (click to toggle)
dosage 3.0%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,276 kB
  • sloc: python: 12,496; sh: 55; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# SPDX-License-Identifier: MIT
# Copied from: https://github.com/pycontribs/tendo
# License: PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
# Author: Sorin Sbarnea
# Changes: changed logging and formatting
from dosagelib import singleton
from multiprocessing import Process


def f(flavor_id):
    return singleton.SingleInstance(flavor_id=flavor_id, exit_code=1)


class TestSingleton(object):
    def test_1(self):
        # test in current process
        me = singleton.SingleInstance(flavor_id="test-1")
        del me  # now the lock should be removed
        assert True

    def test_2(self):
        # test in current subprocess
        p = Process(target=f, args=("test-2",))
        p.start()
        p.join()
        # the called function should succeed
        assert p.exitcode == 0

    def test_3(self):
        # test in current process and subprocess with failure
        # start first instance
        me = f("test-3")
        # second instance
        p = Process(target=f, args=("test-3",))
        p.start()
        p.join()
        assert p.exitcode == 1
        # third instance
        p = Process(target=f, args=("test-3",))
        p.start()
        p.join()
        assert p.exitcode == 1
        del me  # now the lock should be removed