|
| 1 | +import ef |
| 2 | +import sys |
| 3 | +import time |
| 4 | +import math |
| 5 | +import random |
| 6 | +import threading |
| 7 | +import itertools |
| 8 | + |
| 9 | +username, password, channel, sleep_s_str, name_csv = sys.argv[1:] |
| 10 | +sleep_s = int(sleep_s_str) |
| 11 | +names = [name.strip() for name in name_csv.split(",")] |
| 12 | + |
| 13 | +print "starting with", username, password, channel, names |
| 14 | +c = ef.Client("http://localhost:8080/ef/api/", username, password) |
| 15 | + |
| 16 | +print(c.login()) |
| 17 | + |
| 18 | +def now(): |
| 19 | + return int(time.time() * 1000) |
| 20 | + |
| 21 | +START = "start" |
| 22 | +STARTED = "started" |
| 23 | +ERROR = "error" |
| 24 | +ABORTED = "aborted" |
| 25 | +FINISHED = "finished" |
| 26 | + |
| 27 | +STATES = { |
| 28 | + START: [STARTED], |
| 29 | + STARTED: [ERROR, ABORTED, FINISHED], |
| 30 | + ERROR: [START], |
| 31 | + ABORTED: [START], |
| 32 | + FINISHED: [START] |
| 33 | +} |
| 34 | + |
| 35 | +MAX_SLEEP_BY_STATE = { |
| 36 | + START: int(sleep_s / 3.0) + 1, |
| 37 | + STARTED: sleep_s, |
| 38 | + ERROR: sleep_s, |
| 39 | + ABORTED: sleep_s, |
| 40 | + FINISHED: sleep_s |
| 41 | +} |
| 42 | + |
| 43 | +ERROR_REASONS = [ |
| 44 | + "request cannot be fulfilled due to bad syntax", |
| 45 | + "authentication is possible but has failed", |
| 46 | + "payment required, reserved for future use", |
| 47 | + "server refuses to respond to request", |
| 48 | + "requested resource could not be found", |
| 49 | + "request method not supported by that resource", |
| 50 | + "content not acceptable according to the Accept headers", |
| 51 | + "client must first authenticate itself with the proxy", |
| 52 | + "server timed out waiting for the request", |
| 53 | + "request could not be processed because of conflict", |
| 54 | + "resource is no longer available and will not be available again", |
| 55 | + "request did not specify the length of its content", |
| 56 | + "server does not meet request preconditions", |
| 57 | + "request is larger than the server is willing or able to process", |
| 58 | + "URI provided was too long for the server to process", |
| 59 | + "server does not support media type", |
| 60 | + "client has asked for unprovidable portion of the file", |
| 61 | + "server cannot meet requirements of Expect request-header field", |
| 62 | + "request unable to be followed due to semantic errors", |
| 63 | + "resource that is being accessed is locked", |
| 64 | + "request failed due to failure of a previous request", |
| 65 | + "client should switch to a different protocol", |
| 66 | + "origin server requires the request to be conditional", |
| 67 | + "user has sent too many requests in a given amount of time", |
| 68 | + "server is unwilling to process the request", |
| 69 | + "server returns no information and closes the connection", |
| 70 | + "request should be retried after performing action", |
| 71 | + "Windows Parental Controls blocking access to webpage", |
| 72 | + "The server cannot reach the client's mailbox.", |
| 73 | + "connection closed by client while HTTP server is processing", |
| 74 | + "server does not recognise method or lacks ability to fulfill", |
| 75 | + "server received an invalid response from upstream server", |
| 76 | + "server is currently unavailable", |
| 77 | + "gateway did not receive response from upstream server", |
| 78 | + "server does not support the HTTP protocol version", |
| 79 | + "content negotiation for the request results in a circular reference", |
| 80 | + "server is unable to store the representation", |
| 81 | + "server detected an infinite loop while processing the request", |
| 82 | + "bandwidth limit exceeded", |
| 83 | + "further extensions to the request are required", |
| 84 | + "client needs to authenticate to gain network access", |
| 85 | + "network read timeout behind the proxy", |
| 86 | + "network connect timeout behind the proxy"] |
| 87 | + |
| 88 | + |
| 89 | +class ServiceRunner(threading.Thread): |
| 90 | + def __init__(self, name): |
| 91 | + threading.Thread.__init__(self) |
| 92 | + self.name = name |
| 93 | + self.state = START |
| 94 | + self.started = None |
| 95 | + self.setDaemon(True) |
| 96 | + |
| 97 | + def run(self): |
| 98 | + while True: |
| 99 | + next_state = random.choice(STATES[self.state]) |
| 100 | + sleep_time = random.randint(1, MAX_SLEEP_BY_STATE[self.state]) |
| 101 | + print "sleeping", sleep_time, "seconds" |
| 102 | + time.sleep(sleep_time) |
| 103 | + |
| 104 | + value = { |
| 105 | + "name": self.name, |
| 106 | + "state": self.state, |
| 107 | + "started": self.started |
| 108 | + } |
| 109 | + |
| 110 | + if self.state == ERROR: |
| 111 | + value["reason"] = random.choice(ERROR_REASONS) |
| 112 | + elif self.state == START: |
| 113 | + self.started = now() |
| 114 | + |
| 115 | + if self.state in (ERROR, ABORTED, FINISHED): |
| 116 | + value["ended"] = now() |
| 117 | + |
| 118 | + event = ef.Event(value, channel, username) |
| 119 | + |
| 120 | + if self.state != START: |
| 121 | + print c.send_event(event) |
| 122 | + |
| 123 | + self.state = next_state |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + for name in names: |
| 127 | + service_runner = ServiceRunner(name) |
| 128 | + service_runner.start() |
| 129 | + |
| 130 | + while True: |
| 131 | + try: |
| 132 | + time.sleep(1) |
| 133 | + except KeyboardInterrupt: |
| 134 | + print "closing" |
| 135 | + break |
0 commit comments