Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / tests / test-reconnect.py
1 # Copyright (c) 2009, 2010, 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import errno
16 import sys
17
18 import ovs.reconnect
19
20 now = 0
21 r = None
22
23
24 def do_enable(_):
25     r.enable(now)
26
27
28 def do_disable(_):
29     r.disable(now)
30
31
32 def do_force_reconnect(_):
33     r.force_reconnect(now)
34
35
36 def error_from_string(s):
37     if not s:
38         return 0
39     elif s == "ECONNREFUSED":
40         return errno.ECONNREFUSED
41     elif s == "EOF":
42         return ovs.reconnect.EOF
43     else:
44         sys.stderr.write("unknown error '%s'\n" % s)
45         sys.exit(1)
46
47
48 def do_disconnected(arg):
49     r.disconnected(now, error_from_string(arg))
50
51
52 def do_connecting(_):
53     r.connecting(now)
54
55
56 def do_connect_failed(arg):
57     r.connect_failed(now, error_from_string(arg))
58
59
60 def do_connected(_):
61     r.connected(now)
62
63
64 def do_activity(_):
65     r.activity(now)
66
67
68 def do_run(arg):
69     global now
70     if arg is not None:
71         now += int(arg)
72
73     action = r.run(now)
74     if action is None:
75         pass
76     elif action == ovs.reconnect.CONNECT:
77         print "  should connect"
78     elif action == ovs.reconnect.DISCONNECT:
79         print "  should disconnect"
80     elif action == ovs.reconnect.PROBE:
81         print "  should send probe"
82     else:
83         assert False
84
85
86 def do_advance(arg):
87     global now
88     now += int(arg)
89
90
91 def do_timeout(_):
92     global now
93     timeout = r.timeout(now)
94     if timeout >= 0:
95         print "  advance %d ms" % timeout
96         now += timeout
97     else:
98         print "  no timeout"
99
100
101 def do_set_max_tries(arg):
102     r.set_max_tries(int(arg))
103
104
105 def diff_stats(old, new, delta):
106     if (old.state != new.state or
107         old.state_elapsed != new.state_elapsed or
108         old.backoff != new.backoff):
109         print("  in %s for %d ms (%d ms backoff)"
110               % (new.state, new.state_elapsed, new.backoff))
111
112     if (old.creation_time != new.creation_time or
113         old.last_activity != new.last_activity or
114         old.last_connected != new.last_connected):
115         print("  created %d, last activity %d, last connected %d"
116               % (new.creation_time, new.last_activity, new.last_connected))
117
118     if (old.n_successful_connections != new.n_successful_connections or
119         old.n_attempted_connections != new.n_attempted_connections or
120         old.seqno != new.seqno):
121         print("  %d successful connections out of %d attempts, seqno %d"
122               % (new.n_successful_connections, new.n_attempted_connections,
123                  new.seqno))
124
125     if (old.is_connected != new.is_connected):
126         if new.is_connected:
127             negate = ""
128         else:
129             negate = "dis"
130         print("  %sconnected" % negate)
131
132     if (old.last_connected != new.last_connected or
133         (new.msec_since_connect != None and
134          old.msec_since_connect != new.msec_since_connect - delta) or
135         (old.total_connected_duration != new.total_connected_duration - delta
136             and not (old.total_connected_duration == 0 and
137                 new.total_connected_duration == 0))):
138         print("  last connected %d ms ago, connected %d ms total"
139               % (new.msec_since_connect, new.total_connected_duration))
140
141     if (old.last_disconnected != new.last_disconnected or
142         (new.msec_since_disconnect != None and
143          old.msec_since_disconnect != new.msec_since_disconnect - delta)):
144         print("  disconnected at %d ms (%d ms ago)"
145               % (new.last_disconnected, new.msec_since_disconnect))
146
147
148 def do_set_passive(_):
149     r.set_passive(True, now)
150
151
152 def do_listening(_):
153     r.listening(now)
154
155
156 def do_listen_error(arg):
157     r.listen_error(now, int(arg))
158
159
160 def main():
161     commands = {
162         "enable": do_enable,
163         "disable": do_disable,
164         "force-reconnect": do_force_reconnect,
165         "disconnected": do_disconnected,
166         "connecting": do_connecting,
167         "connect-failed": do_connect_failed,
168         "connected": do_connected,
169         "activity": do_activity,
170         "run": do_run,
171         "advance": do_advance,
172         "timeout": do_timeout,
173         "set-max-tries": do_set_max_tries,
174         "passive": do_set_passive,
175         "listening": do_listening,
176         "listen-error": do_listen_error
177     }
178
179     global now
180     global r
181
182     now = 1000
183     r = ovs.reconnect.Reconnect(now)
184     r.set_name("remote")
185     prev = r.get_stats(now)
186     print "### t=%d ###" % now
187     old_time = now
188     old_max_tries = r.get_max_tries()
189     while True:
190         line = sys.stdin.readline()
191         if line == "":
192             break
193
194         print line[:-1]
195         if line[0] == "#":
196             continue
197
198         args = line.split()
199         if len(args) == 0:
200             continue
201
202         command = args[0]
203         if len(args) > 1:
204             op = args[1]
205         else:
206             op = None
207         commands[command](op)
208
209         if old_time != now:
210             print
211             print "### t=%d ###" % now
212
213         cur = r.get_stats(now)
214         diff_stats(prev, cur, now - old_time)
215         prev = cur
216         if r.get_max_tries() != old_max_tries:
217             old_max_tries = r.get_max_tries()
218             print "  %d tries left" % old_max_tries
219
220         old_time = now
221
222
223 if __name__ == '__main__':
224     main()