python: Style cleanup.
[sliver-openvswitch.git] / tests / test-reconnect.py
1 # Copyright (c) 2009, 2010 Nicira Networks.
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 logging
17 import sys
18
19 import ovs.reconnect
20
21 now = 0
22 r = None
23
24
25 def do_enable(_):
26     r.enable(now)
27
28
29 def do_disable(_):
30     r.disable(now)
31
32
33 def do_force_reconnect(_):
34     r.force_reconnect(now)
35
36
37 def error_from_string(s):
38     if not s:
39         return 0
40     elif s == "ECONNREFUSED":
41         return errno.ECONNREFUSED
42     elif s == "EOF":
43         return ovs.reconnect.EOF
44     else:
45         sys.stderr.write("unknown error '%s'\n" % s)
46         sys.exit(1)
47
48
49 def do_disconnected(arg):
50     r.disconnected(now, error_from_string(arg))
51
52
53 def do_connecting(_):
54     r.connecting(now)
55
56
57 def do_connect_failed(arg):
58     r.connect_failed(now, error_from_string(arg))
59
60
61 def do_connected(_):
62     r.connected(now)
63
64
65 def do_received(_):
66     r.received(now)
67
68
69 def do_run(arg):
70     global now
71     if arg is not None:
72         now += int(arg)
73
74     action = r.run(now)
75     if action is None:
76         pass
77     elif action == ovs.reconnect.CONNECT:
78         print "  should connect"
79     elif action == ovs.reconnect.DISCONNECT:
80         print "  should disconnect"
81     elif action == ovs.reconnect.PROBE:
82         print "  should send probe"
83     else:
84         assert False
85
86
87 def do_advance(arg):
88     global now
89     now += int(arg)
90
91
92 def do_timeout(_):
93     global now
94     timeout = r.timeout(now)
95     if timeout >= 0:
96         print "  advance %d ms" % timeout
97         now += timeout
98     else:
99         print "  no timeout"
100
101
102 def do_set_max_tries(arg):
103     r.set_max_tries(int(arg))
104
105
106 def diff_stats(old, new, delta):
107     if (old.state != new.state or
108         old.state_elapsed != new.state_elapsed or
109         old.backoff != new.backoff):
110         print("  in %s for %d ms (%d ms backoff)"
111               % (new.state, new.state_elapsed, new.backoff))
112
113     if (old.creation_time != new.creation_time or
114         old.last_received != new.last_received or
115         old.last_connected != new.last_connected):
116         print("  created %d, last received %d, last connected %d"
117               % (new.creation_time, new.last_received, new.last_connected))
118
119     if (old.n_successful_connections != new.n_successful_connections or
120         old.n_attempted_connections != new.n_attempted_connections or
121         old.seqno != new.seqno):
122         print("  %d successful connections out of %d attempts, seqno %d"
123               % (new.n_successful_connections, new.n_attempted_connections,
124                  new.seqno))
125
126     if (old.is_connected != new.is_connected):
127         if new.is_connected:
128             negate = ""
129         else:
130             negate = "dis"
131         print("  %sconnected" % negate)
132
133     if (old.last_connected != new.last_connected or
134         (new.msec_since_connect != None and
135          old.msec_since_connect != new.msec_since_connect - delta) or
136         (old.total_connected_duration != new.total_connected_duration - delta
137             and not (old.total_connected_duration == 0 and
138                 new.total_connected_duration == 0))):
139         print("  last connected %d ms ago, connected %d ms total"
140               % (new.msec_since_connect, new.total_connected_duration))
141
142     if (old.last_disconnected != new.last_disconnected or
143         (new.msec_since_disconnect != None and
144          old.msec_since_disconnect != new.msec_since_disconnect - delta)):
145         print("  disconnected at %d ms (%d ms ago)"
146               % (new.last_disconnected, new.msec_since_disconnect))
147
148
149 def do_set_passive(_):
150     r.set_passive(True, now)
151
152
153 def do_listening(_):
154     r.listening(now)
155
156
157 def do_listen_error(arg):
158     r.listen_error(now, int(arg))
159
160
161 def main():
162     commands = {
163         "enable": do_enable,
164         "disable": do_disable,
165         "force-reconnect": do_force_reconnect,
166         "disconnected": do_disconnected,
167         "connecting": do_connecting,
168         "connect-failed": do_connect_failed,
169         "connected": do_connected,
170         "received": do_received,
171         "run": do_run,
172         "advance": do_advance,
173         "timeout": do_timeout,
174         "set-max-tries": do_set_max_tries,
175         "passive": do_set_passive,
176         "listening": do_listening,
177         "listen-error": do_listen_error
178     }
179
180     logging.basicConfig(level=logging.CRITICAL)
181
182     global now
183     global r
184
185     now = 1000
186     r = ovs.reconnect.Reconnect(now)
187     r.set_name("remote")
188     prev = r.get_stats(now)
189     print "### t=%d ###" % now
190     old_time = now
191     old_max_tries = r.get_max_tries()
192     while True:
193         line = sys.stdin.readline()
194         if line == "":
195             break
196
197         print line[:-1]
198         if line[0] == "#":
199             continue
200
201         args = line.split()
202         if len(args) == 0:
203             continue
204
205         command = args[0]
206         if len(args) > 1:
207             op = args[1]
208         else:
209             op = None
210         commands[command](op)
211
212         if old_time != now:
213             print
214             print "### t=%d ###" % now
215
216         cur = r.get_stats(now)
217         diff_stats(prev, cur, now - old_time)
218         prev = cur
219         if r.get_max_tries() != old_max_tries:
220             old_max_tries = r.get_max_tries()
221             print "  %d tries left" % old_max_tries
222
223         old_time = now
224
225
226 if __name__ == '__main__':
227     main()