summaryrefslogtreecommitdiff
path: root/tests/causalorderingtest.py
blob: 4b2dc053aeab0a9ed8cc9786ef161a76fa5e8f56 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python

from twisted.internet import reactor
from mesh import Mesh
from mesh import MeshNode
import sys

nodes = []
# We're optimists
success = True

class TestMeshNode(MeshNode):
  num = 1

  def newNode (self, data):
    self.num += 1
    if len (nodes) == self.num:
      reactor.callLater(1.5, (lambda y: nodes[0].pushInput("0\n")), x)


class TestMesh(Mesh):
  done = 0
  expected = None

  def gotOutput(self, node, sender, data):
    global success
    value = int(data.rstrip())
    if (node in self.nodes[0:3]):
      if (self.nodes.index(node) == (value  + 1) % 3):
        reactor.callLater(0.1,
            (lambda: node.pushInput( str(value + 1) +  "\n")))
    else:
      print node.name + " - " + sender + " - " + data.rstrip()
      if self.expected == None:
        self.expected = value
      if self.expected > 2 and self.expected != value:
        print "Expected: " + str(self.expected) + " But got: " + str(value)
        success = False
        reactor.crash()
      self.expected = value + 1

      if self.expected > 50:
        reactor.crash()

m = TestMesh()

for x in xrange(0, 3):
  n = MeshNode("node" + str(x), m)
  nodes.append(n)
  m.addMeshNode(n)

n = TestMeshNode("node3", m)
nodes.append(n)
m.addMeshNode(n)


#connect node 0 and 1 together with dropfree links, and  0 <->2 and 1 <-> 2
# with quite lossy links
m.connect_duplex(nodes[0], nodes[1], 100, 0, 0)
m.connect_duplex(nodes[0], nodes[2], 100, 0, 0)

m.connect_duplex(nodes[1], nodes[2], 100, 0, 0)

m.connect_duplex(nodes[0], nodes[3], 100, 0, 0.50)
m.connect_duplex(nodes[1], nodes[3], 100, 0, 0.50)
m.connect_duplex(nodes[2], nodes[3], 100, 0, 0.50)

def timeout():
  global success
  print "TIMEOUT!"
  success = False
  reactor.crash()

reactor.callLater(60, timeout)

reactor.run()


if not success:
  sys.stderr.write("FAILED\n")
  sys.exit(-1)
print "SUCCESS"