def hallo_getter(): # Create a Hallo hallo = Hallo() hallo_thread = Thread(target=hallo.start,) def function(modules=None): if modules is None: modules = { "ascii_art", "bio", "channel_control", "convert", "euler", "hallo_control", "math", "permission_control", "random", "server_control", "silly", "subscriptions", } function_dispatcher = FunctionDispatcher(modules, hallo) hallo.function_dispatcher = function_dispatcher # Create server server = ServerMock(hallo) server.name = "mock-server" hallo.add_server(server) # Start hallo thread hallo_thread.start() # Create test users and channel, and configure them hallo_user = server.get_user_by_address( server.get_nick().lower(), server.get_nick() ) test_user = server.get_user_by_address("test", "test") test_user.online = True test_chan = server.get_channel_by_address("#test", "#test") test_chan.in_channel = True test_chan.add_user(hallo_user) test_chan.add_user(test_user) # Wait until hallo is open count = 0 while not hallo.open: time.sleep(0.01) count += 1 assert count < 1000, "Hallo took too long to start." if count > 1000: break # Clear any data in the server server.get_send_data() return hallo, server, test_chan, test_user yield function hallo.close() hallo_thread.join()
def test_init(): # Create some basic stuff test_modules = {"euler"} test_hallo = Hallo() # Create function dispatcher fd = FunctionDispatcher(test_modules, test_hallo) test_hallo.function_dispatcher = fd try: # Check basic class variable setting assert (fd.hallo == test_hallo ), "Hallo object was not set correctly in FunctionDispatcher." assert ( fd.module_list == test_modules ), "Module list was not imported correctly by FunctionDispatcher." # Check that module reloading has done things assert len(fd.function_dict) == len( test_modules), "Modules were not loaded to function dictionary." assert len(fd.function_names ) != 0, "Functions were not added to function_names" finally: fd.close() test_hallo.close()
class TestBase(unittest.TestCase): def setUp(self): print("Starting test: " + self.id()) self.start_time = time.time() # Create a Hallo self.hallo = Hallo() # Only the required modules, only 1 (mock) server # Todo: specify modules by test? self.function_dispatcher = FunctionDispatcher( {"convert", "random", "server_control", "subscriptions"}, self.hallo ) self.hallo.function_dispatcher = self.function_dispatcher print( "Running test: " + self.id() + ". Init took: " + str(time.time() - self.start_time) + " seconds." ) self.server = ServerMock(self.hallo) self.server.name = "mock-server" self.hallo.add_server(self.server) # Start hallo thread self.hallo_thread = Thread(target=self.hallo.start,) self.hallo_thread.start() # Create test users and channel, and configure them self.hallo_user = self.server.get_user_by_address( self.server.get_nick().lower(), self.server.get_nick() ) self.test_user = self.server.get_user_by_address("test", "test") self.test_user.online = True self.test_chan = self.server.get_channel_by_address("#test", "#test") self.test_chan.in_channel = True self.test_chan.add_user(self.hallo_user) self.test_chan.add_user(self.test_user) # Wait until hallo is open count = 0 while not self.hallo.open: time.sleep(0.01) count += 1 assert count < 1000, "Hallo took too long to start." if count > 1000: break # Clear any data in the server self.server.get_send_data() # Print test startup time print( "Running test: " + self.id() + ". Startup took: " + str(time.time() - self.start_time) + " seconds." ) self.start_time = time.time() def tearDown(self): print( "Finishing test: " + self.id() + ". Test took: " + str(time.time() - self.start_time) + " seconds." ) self.hallo.close() self.hallo_thread.join() print( "Finished test: " + self.id() + ". Test took: " + str(time.time() - self.start_time) + " seconds." ) @classmethod def tearDownClass(cls): del cls gc.collect()