def __init__(self, host, port, nrOfMessages, nrOfClients): ''' Starts test **Arguments** *host* Machine where text service will run *port* Port used by text service on given host *nrOfMessages* Number of text messages sent by each client *nrOfClients* Number of clients sending/receiving messages ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.eventsReceived = [] self.sendLock = ServerLock("send") self.finishedLock = ServerLock("finished") self.nrOfMessages = nrOfMessages self.nrOfClients = nrOfClients self.totalNrOfMessages = self.nrOfClients * self.nrOfClients * self.nrOfMessages + 1 self.clientsFinished = 1 self.index = 1 print "Nr of text clients used:", self.nrOfClients print "Nr of messages per client:", self.nrOfMessages self.ts = TextServiceController(host, port) self.StartClients()
def __init__(self, host, port, nrOfClients, nrOfEvents): ''' Starts test *** Arguments *** *host* Machine where event service will run *port* Port used by event service on given host *nrOfClients* Number of clients receiving events *nrOfEvents* How many of each event will be sent (1 ~ 15 events) ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.nrOfReceivedEvents = 0 self.sendLock = ServerLock("send") self.receiveLock = ServerLock("receive") self.nrOfEvent = nrOfEvents self.nrOfClients = nrOfClients self.clientList = [] self.host = host self.port = port self.index = 1 self.esc = EventServiceController(self) # Sleep to make sure the event client gets started before sending events. time.sleep(2) self.CreateEvents() self.StartClients() time.sleep(5) self.DistributeEvents() # Shut down i = 1 while i: if self.nrOfReceivedEvents == self.totalEvents: self.ShutDown() i = 0
class StartTest: ''' Tests event service and client EventServiceController - Starts event service and distributes events EventServiceClient - Has an event client and receives events Several EventServiceClients can be started that will receive all distributed events. The result shows distributed events and which events got received by which client. ''' def __init__(self, host, port, nrOfClients, nrOfEvents): ''' Starts test *** Arguments *** *host* Machine where event service will run *port* Port used by event service on given host *nrOfClients* Number of clients receiving events *nrOfEvents* How many of each event will be sent (1 ~ 15 events) ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.nrOfReceivedEvents = 0 self.sendLock = ServerLock("send") self.receiveLock = ServerLock("receive") self.nrOfEvent = nrOfEvents self.nrOfClients = nrOfClients self.clientList = [] self.host = host self.port = port self.index = 1 self.esc = EventServiceController(self) # Sleep to make sure the event client gets started before sending events. time.sleep(2) self.CreateEvents() self.StartClients() time.sleep(5) self.DistributeEvents() # Shut down i = 1 while i: if self.nrOfReceivedEvents == self.totalEvents: self.ShutDown() i = 0 def CreateEvents(self): dataDescription = DataDescription3("") profile = ClientProfile() service = ServiceDescription("service", "desc", "uri", "mime") stream = StreamDescription3("") conn = ConnectionDescription("") app = ApplicationDescription(1, 'app', 'desc', 'uri', 'mime') self.sendEvents = [ Events.AddDataEvent(self.esc.GetChannelId(), dataDescription), Events.UpdateDataEvent(self.esc.GetChannelId(), dataDescription), Events.RemoveDataEvent(self.esc.GetChannelId(), dataDescription) ] self.distributeEvents = [ Event(Event.ADD_DATA, self.esc.GetChannelId(), dataDescription), Event(Event.UPDATE_DATA, self.esc.GetChannelId(), dataDescription), Event(Event.REMOVE_DATA, self.esc.GetChannelId(), dataDescription), Event(Event.ENTER, self.esc.GetChannelId(), profile), Event(Event.MODIFY_USER, self.esc.GetChannelId(), profile), Event(Event.EXIT, self.esc.GetChannelId(), profile), Event(Event.ADD_SERVICE, self.esc.GetChannelId(), service), Event(Event.REMOVE_SERVICE, self.esc.GetChannelId(), service), Event(Event.ADD_APPLICATION, self.esc.GetChannelId(), app), Event(Event.REMOVE_APPLICATION, self.esc.GetChannelId(), app), Event(Event.ADD_CONNECTION, self.esc.GetChannelId(), conn), Event(Event.REMOVE_CONNECTION, self.esc.GetChannelId(), conn), Event(Event.ADD_STREAM, self.esc.GetChannelId(), stream), Event(Event.MODIFY_STREAM, self.esc.GetChannelId(), stream), Event(Event.REMOVE_STREAM, self.esc.GetChannelId(), stream) ] # Each client will receive all events. self.totalEvents = self.nrOfClients * len( self.distributeEvents) * self.nrOfEvent print 'Starting one event service and %s event clients' % (nrOfClients) def DistributeEvents(self): index = 0 for i in range(self.nrOfEvent): index = index + 1 for event in self.distributeEvents: event.data.name = " D-" + str(index) self.esc.DistributeEvents(event) def StartClients(self): print "\nWait until clients received all events. If this hangs \nfor more than a minute, some evemts did not get received.\n" for i in range(nrOfClients): client = EventServiceClient(self) client.CreateEventClient(self.esc.GetChannelId(), self.esc.GetLocation()) self.clientList.append(client) def ReceivedEvent(self): self.receiveLock.acquire() self.nrOfReceivedEvents = self.nrOfReceivedEvents + 1 if self.nrOfReceivedEvents == self.totalEvents: self.receiveLock.release() # All events are received self.ShowResult() else: self.receiveLock.release() def ShowResult(self): print '------------ RESULT --------------\n' print "['event type D/S-index'] where \nD = Distributed from event service\nS = Sent from event client\nindex = unique identifier\n" print "Sent/distributed events:\n %s\n" % (self.eventsOut) # Print all messages sent and received for client in self.clientList: print "Client%s received:\n %s\n" % (client.GetName(), client.eventReceivedList) rightOrder = 1 receivedAllEvts = 1 for client in self.clientList: notReceived = [] for event in self.eventsOut: if event not in client.eventReceivedList: receivedAllEvts = 0 notReceived.append(event) if len(notReceived) > 0: print 'client' + client.GetName( ) + " did not receive messages: " + str(notReceived) if not self.eventsOut == client.eventReceivedList: rightOrder = 0 print 'client' + client.GetName( ) + ' did not receive events in right order.\n' if receivedAllEvts: print '*** SUCCESS - All events sent got received. ***' else: print '*** FAIL - All events did not get received. ***' if rightOrder: print '*** SUCCESS - All events sent got received in right order. ***' else: print '*** FAIL - All events did NOT get received in right order. ***' print '--------------------------------------\n' def ShutDown(self): '''Shut down service and clients''' print 'Shut down' for client in self.clientList: client.ShutDown() self.esc.ShutDown()
class StartTest: ''' Tests event service and client EventServiceController - Starts event service and distributes events EventServiceClient - Has an event client and receives events Several EventServiceClients can be started that will receive all distributed events. The result shows distributed events and which events got received by which client. ''' def __init__(self, host, port, nrOfClients, nrOfEvents): ''' Starts test *** Arguments *** *host* Machine where event service will run *port* Port used by event service on given host *nrOfClients* Number of clients receiving events *nrOfEvents* How many of each event will be sent (1 ~ 15 events) ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.nrOfReceivedEvents = 0 self.sendLock = ServerLock("send") self.receiveLock = ServerLock("receive") self.nrOfEvent = nrOfEvents self.nrOfClients = nrOfClients self.clientList = [] self.host = host self.port = port self.index = 1 self.esc = EventServiceController(self) # Sleep to make sure the event client gets started before sending events. time.sleep(2) self.CreateEvents() self.StartClients() time.sleep(5) self.DistributeEvents() # Shut down i = 1 while i: if self.nrOfReceivedEvents == self.totalEvents: self.ShutDown() i = 0 def CreateEvents(self): dataDescription = DataDescription3("") profile = ClientProfile() service = ServiceDescription("service", "desc", "uri", "mime") stream = StreamDescription3("") conn = ConnectionDescription("") app = ApplicationDescription(1, 'app', 'desc', 'uri', 'mime') self.sendEvents = [Events.AddDataEvent(self.esc.GetChannelId(), dataDescription), Events.UpdateDataEvent(self.esc.GetChannelId(), dataDescription), Events.RemoveDataEvent(self.esc.GetChannelId(), dataDescription)] self.distributeEvents = [Event( Event.ADD_DATA, self.esc.GetChannelId(), dataDescription ), Event( Event.UPDATE_DATA, self.esc.GetChannelId(), dataDescription ), Event( Event.REMOVE_DATA, self.esc.GetChannelId(), dataDescription ), Event( Event.ENTER, self.esc.GetChannelId(), profile) , Event( Event.MODIFY_USER, self.esc.GetChannelId(), profile) , Event( Event.EXIT, self.esc.GetChannelId(), profile) , Event( Event.ADD_SERVICE, self.esc.GetChannelId(), service), Event( Event.REMOVE_SERVICE, self.esc.GetChannelId(), service), Event( Event.ADD_APPLICATION, self.esc.GetChannelId(), app), Event( Event.REMOVE_APPLICATION, self.esc.GetChannelId(), app) , Event( Event.ADD_CONNECTION, self.esc.GetChannelId(), conn) , Event( Event.REMOVE_CONNECTION, self.esc.GetChannelId(), conn), Event( Event.ADD_STREAM, self.esc.GetChannelId(), stream), Event( Event.MODIFY_STREAM, self.esc.GetChannelId(), stream), Event( Event.REMOVE_STREAM, self.esc.GetChannelId(), stream)] # Each client will receive all events. self.totalEvents = self.nrOfClients * len(self.distributeEvents) * self.nrOfEvent print 'Starting one event service and %s event clients' %(nrOfClients) def DistributeEvents(self): index = 0 for i in range(self.nrOfEvent): index = index + 1 for event in self.distributeEvents: event.data.name =" D-"+str(index) self.esc.DistributeEvents(event) def StartClients(self): print "\nWait until clients received all events. If this hangs \nfor more than a minute, some evemts did not get received.\n" for i in range(nrOfClients): client = EventServiceClient(self) client.CreateEventClient(self.esc.GetChannelId(), self.esc.GetLocation()) self.clientList.append(client) def ReceivedEvent(self): self.receiveLock.acquire() self.nrOfReceivedEvents = self.nrOfReceivedEvents + 1 if self.nrOfReceivedEvents == self.totalEvents: self.receiveLock.release() # All events are received self.ShowResult() else: self.receiveLock.release() def ShowResult(self): print '------------ RESULT --------------\n' print "['event type D/S-index'] where \nD = Distributed from event service\nS = Sent from event client\nindex = unique identifier\n" print "Sent/distributed events:\n %s\n"%(self.eventsOut) # Print all messages sent and received for client in self.clientList: print "Client%s received:\n %s\n"%(client.GetName(), client.eventReceivedList) rightOrder = 1 receivedAllEvts = 1 for client in self.clientList: notReceived = [] for event in self.eventsOut: if event not in client.eventReceivedList: receivedAllEvts = 0 notReceived.append(event) if len(notReceived)>0: print 'client'+client.GetName()+ " did not receive messages: "+str(notReceived) if not self.eventsOut == client.eventReceivedList: rightOrder = 0 print 'client'+client.GetName()+ ' did not receive events in right order.\n' if receivedAllEvts: print '*** SUCCESS - All events sent got received. ***' else: print '*** FAIL - All events did not get received. ***' if rightOrder: print '*** SUCCESS - All events sent got received in right order. ***' else: print '*** FAIL - All events did NOT get received in right order. ***' print '--------------------------------------\n' def ShutDown(self): '''Shut down service and clients''' print 'Shut down' for client in self.clientList: client.ShutDown() self.esc.ShutDown()
class StartTest: ''' Tests text service and client TextServiceController - Starts text service TextServiceClient - Has a text client and sends/receives text messages Several TextServiceClients are created and are sending messages in separate threads. In order to show correct result, we are waiting for all TextServiceClients to receive all text messages sent. The result shows sent and received messages in the order they got sent/received. ISSUES: * If the text service has shut down before the event clients have properly disconnected, an exception will be raised. Solution; use sleep to make sure they have exited. * If the text clients try to disconnect while still receiving text messages, an exception will be raised. Solution: wait to show result and shut down until all clients have received all messages. * If the send threads are started before the text clients have properly connected, some events might not get received. Solution; use sleep to make sure clients have connected. ''' def __init__(self, host, port, nrOfMessages, nrOfClients): ''' Starts test **Arguments** *host* Machine where text service will run *port* Port used by text service on given host *nrOfMessages* Number of text messages sent by each client *nrOfClients* Number of clients sending/receiving messages ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.eventsReceived = [] self.sendLock = ServerLock("send") self.finishedLock = ServerLock("finished") self.nrOfMessages = nrOfMessages self.nrOfClients = nrOfClients self.totalNrOfMessages = self.nrOfClients * self.nrOfClients * self.nrOfMessages + 1 self.clientsFinished = 1 self.index = 1 print "Nr of text clients used:", self.nrOfClients print "Nr of messages per client:", self.nrOfMessages self.ts = TextServiceController(host, port) self.StartClients() def StartClients(self): '''Creates text clients that send text messages in separate threads.''' # Create text clients self.tscList = [] for i in range(self.nrOfClients): tsc = TextServiceClient(self) tsc.CreateTextClient(self.ts.GetChannelId(), self.ts.GetLocation()) self.tscList.append(tsc) # Make sure the text client is properly started # else some events might not get received time.sleep(1) # Start clients send method in threads print '\nStart sending text messages.' print "\nWait until all clients received all messages. If this hangs \nfor more than a minute, some messages did not get received.\n" self.threadList = [] for tsc in self.tscList: thread = threading.Thread(target = tsc.SendText) self.threadList.append(thread) for thread in self.threadList: thread.start() # Join threads for thread in self.threadList: thread.join() # Shut down i = 1 while i: if self.clientsFinished == self.totalNrOfMessages: self.ShutDown() i = 0 def ClientReceived(self): ''' Is called by a text client when it has receives a message. When all messages are received, show the result. ''' self.finishedLock.acquire() self.clientsFinished = self.clientsFinished + 1 if self.clientsFinished == self.totalNrOfMessages: # Each client gets each sent message # When all messages are received, show result and shut down self.finishedLock.release() self.ShowResult() else: self.finishedLock.release() def ShowResult(self): '''Prints the result''' print '--- RESULT ---' print "Sent text messages:\n %s\n"%(self.eventsOut) # Print all messages sent and received for client in self.tscList: print "%s received:\n %s\n"%(client.profile.name, client.receiveList) receivedAllEvts = 1 rightOrder = 1 # For each client, check if it received all messages and if messages received # matches the order of messages sent. for client in self.tscList: notReceived = [] for event in self.eventsOut: if event not in client.receiveList: receivedAllEvts = 0 notReceived.append(event) if len(notReceived)>0: print client.profile.name+ " did not receive messages: "+str(notReceived) if not self.eventsOut == client.receiveList: rightOrder = 0 print client.profile.name + ' did not receive events in right order.\n' # Print result if receivedAllEvts: print '*** SUCCESS - All events sent got received. ***' else: print '*** FAIL - All events did not get received. ***' if rightOrder: print '*** SUCCESS - All events sent got received in right order. ***' else: print '*** FAIL - All events did NOT get received in right order. ***' print '--------------\n' def ShutDown(self): 'shut down clients and service' for tsc in self.tscList: tsc.ShutDown() time.sleep(self.nrOfClients) # NOTE: If we don't sleep here, the service will start to shut down before # the clients have finished disconnecting. This will cause the text # service exit to raise an exception. self.ts.ShutDown()
class StartTest: ''' Tests text service and client TextServiceController - Starts text service TextServiceClient - Has a text client and sends/receives text messages Several TextServiceClients are created and are sending messages in separate threads. In order to show correct result, we are waiting for all TextServiceClients to receive all text messages sent. The result shows sent and received messages in the order they got sent/received. ISSUES: * If the text service has shut down before the event clients have properly disconnected, an exception will be raised. Solution; use sleep to make sure they have exited. * If the text clients try to disconnect while still receiving text messages, an exception will be raised. Solution: wait to show result and shut down until all clients have received all messages. * If the send threads are started before the text clients have properly connected, some events might not get received. Solution; use sleep to make sure clients have connected. ''' def __init__(self, host, port, nrOfMessages, nrOfClients): ''' Starts test **Arguments** *host* Machine where text service will run *port* Port used by text service on given host *nrOfMessages* Number of text messages sent by each client *nrOfClients* Number of clients sending/receiving messages ''' print '**************************************************' print 'Test Started\nThis test will take about 30 sec. to finish' print '**************************************************\n' self.eventsOut = [] self.eventsReceived = [] self.sendLock = ServerLock("send") self.finishedLock = ServerLock("finished") self.nrOfMessages = nrOfMessages self.nrOfClients = nrOfClients self.totalNrOfMessages = self.nrOfClients * self.nrOfClients * self.nrOfMessages + 1 self.clientsFinished = 1 self.index = 1 print "Nr of text clients used:", self.nrOfClients print "Nr of messages per client:", self.nrOfMessages self.ts = TextServiceController(host, port) self.StartClients() def StartClients(self): '''Creates text clients that send text messages in separate threads.''' # Create text clients self.tscList = [] for i in range(self.nrOfClients): tsc = TextServiceClient(self) tsc.CreateTextClient(self.ts.GetChannelId(), self.ts.GetLocation()) self.tscList.append(tsc) # Make sure the text client is properly started # else some events might not get received time.sleep(1) # Start clients send method in threads print '\nStart sending text messages.' print "\nWait until all clients received all messages. If this hangs \nfor more than a minute, some messages did not get received.\n" self.threadList = [] for tsc in self.tscList: thread = threading.Thread(target=tsc.SendText) self.threadList.append(thread) for thread in self.threadList: thread.start() # Join threads for thread in self.threadList: thread.join() # Shut down i = 1 while i: if self.clientsFinished == self.totalNrOfMessages: self.ShutDown() i = 0 def ClientReceived(self): ''' Is called by a text client when it has receives a message. When all messages are received, show the result. ''' self.finishedLock.acquire() self.clientsFinished = self.clientsFinished + 1 if self.clientsFinished == self.totalNrOfMessages: # Each client gets each sent message # When all messages are received, show result and shut down self.finishedLock.release() self.ShowResult() else: self.finishedLock.release() def ShowResult(self): '''Prints the result''' print '--- RESULT ---' print "Sent text messages:\n %s\n" % (self.eventsOut) # Print all messages sent and received for client in self.tscList: print "%s received:\n %s\n" % (client.profile.name, client.receiveList) receivedAllEvts = 1 rightOrder = 1 # For each client, check if it received all messages and if messages received # matches the order of messages sent. for client in self.tscList: notReceived = [] for event in self.eventsOut: if event not in client.receiveList: receivedAllEvts = 0 notReceived.append(event) if len(notReceived) > 0: print client.profile.name + " did not receive messages: " + str( notReceived) if not self.eventsOut == client.receiveList: rightOrder = 0 print client.profile.name + ' did not receive events in right order.\n' # Print result if receivedAllEvts: print '*** SUCCESS - All events sent got received. ***' else: print '*** FAIL - All events did not get received. ***' if rightOrder: print '*** SUCCESS - All events sent got received in right order. ***' else: print '*** FAIL - All events did NOT get received in right order. ***' print '--------------\n' def ShutDown(self): 'shut down clients and service' for tsc in self.tscList: tsc.ShutDown() time.sleep(self.nrOfClients) # NOTE: If we don't sleep here, the service will start to shut down before # the clients have finished disconnecting. This will cause the text # service exit to raise an exception. self.ts.ShutDown()