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