Pārlūkot izejas kodu

add terminal test

Bastien Sevajol 8 gadus atpakaļ
vecāks
revīzija
5f2ac56cd5
1 mainītis faili ar 40 papildinājumiem un 6 dzēšanām
  1. 40 6
      tests/test_terminals.py

+ 40 - 6
tests/test_terminals.py Parādīt failu

6
 from tests import BaseTest
6
 from tests import BaseTest
7
 
7
 
8
 
8
 
9
-class FakeTerminal(Terminal):
9
+class MultiplyTerminal(Terminal):
10
     def receive(self, package: TerminalPackage):
10
     def receive(self, package: TerminalPackage):
11
         self.send(TerminalPackage(package.value * 2))
11
         self.send(TerminalPackage(package.value * 2))
12
         self.send(TerminalPackage(package.value * 4))
12
         self.send(TerminalPackage(package.value * 4))
13
 
13
 
14
 
14
 
15
+class DivideTerminal(Terminal):
16
+    def receive(self, package: TerminalPackage):
17
+        self.send(TerminalPackage(package.value / 2))
18
+        self.send(TerminalPackage(package.value / 4))
19
+
20
+
15
 class TestTerminals(BaseTest):
21
 class TestTerminals(BaseTest):
16
-    def test_terminals_communications(self):
22
+    def test_terminal_communications(self):
17
         terminals_manager = TerminalManager(
23
         terminals_manager = TerminalManager(
18
             terminals=[
24
             terminals=[
19
-                FakeTerminal(),
25
+                MultiplyTerminal(),
20
             ]
26
             ]
21
         )
27
         )
22
         terminals_manager.start()
28
         terminals_manager.start()
24
 
30
 
25
         # We wait max 2s (see time.sleep) to consider
31
         # We wait max 2s (see time.sleep) to consider
26
         # process have finished. If not, it will fail
32
         # process have finished. If not, it will fail
33
+        packages = []
27
         for i in range(200):
34
         for i in range(200):
28
-            packages = terminals_manager.receive()
29
-            if packages:
35
+            packages.extend(terminals_manager.receive())
36
+            if len(packages) == 2:
30
                 break
37
                 break
31
             time.sleep(0.01)
38
             time.sleep(0.01)
32
 
39
 
36
         assert 168 in values
43
         assert 168 in values
37
 
44
 
38
         terminals_manager.stop()  # pytest must execute this if have fail
45
         terminals_manager.stop()  # pytest must execute this if have fail
39
-        # TODO: Tester avec plusieurs terminaux
46
+
47
+    def test_terminals_communications(self):
48
+        terminals_manager = TerminalManager(
49
+            terminals=[
50
+                MultiplyTerminal(),
51
+                DivideTerminal(),
52
+            ]
53
+        )
54
+        terminals_manager.start()
55
+        terminals_manager.send(TerminalPackage(42))
56
+
57
+        # We wait max 2s (see time.sleep) to consider
58
+        # process have finished. If not, it will fail
59
+        packages = []
60
+        for i in range(200):
61
+            packages.extend(terminals_manager.receive())
62
+            if len(packages) == 4:
63
+                break
64
+            time.sleep(0.01)
65
+
66
+        assert 4 == len(packages)
67
+        values = [p.value for p in packages]
68
+        assert 84 in values
69
+        assert 168 in values
70
+        assert 21 in values
71
+        assert 10.5 in values
72
+
73
+        terminals_manager.stop()  # pytest must execute this if have fail