Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
at main 128 lines 4.6 kB view raw
1import pytest 2 3from Worker import WorkerTaskManager 4from . import Spy 5 6 7class TestUiWebsocket: 8 def checkSort(self, tasks): # Check if it has the same order as a list sorted separately 9 tasks_list = list(tasks) 10 tasks_list.sort(key=lambda task: task["id"]) 11 assert tasks_list != list(tasks) 12 tasks_list.sort(key=lambda task: (0 - (task["priority"] - task["workers_num"] * 10), task["id"])) 13 assert tasks_list == list(tasks) 14 15 def testAppendSimple(self): 16 tasks = WorkerTaskManager.WorkerTaskManager() 17 tasks.append({"id": 1, "priority": 15, "workers_num": 1, "inner_path": "file1.json"}) 18 tasks.append({"id": 2, "priority": 1, "workers_num": 0, "inner_path": "file2.json"}) 19 tasks.append({"id": 3, "priority": 8, "workers_num": 0, "inner_path": "file3.json"}) 20 assert [task["inner_path"] for task in tasks] == ["file3.json", "file1.json", "file2.json"] 21 22 self.checkSort(tasks) 23 24 def testAppendMany(self): 25 tasks = WorkerTaskManager.WorkerTaskManager() 26 for i in range(1000): 27 tasks.append({"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i}) 28 assert tasks[0]["inner_path"] == "file39.json" 29 assert tasks[-1]["inner_path"] == "file980.json" 30 31 self.checkSort(tasks) 32 33 def testRemove(self): 34 tasks = WorkerTaskManager.WorkerTaskManager() 35 for i in range(1000): 36 tasks.append({"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i}) 37 38 i = 333 39 task = {"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i} 40 assert task in tasks 41 42 with Spy.Spy(tasks, "indexSlow") as calls: 43 tasks.remove(task) 44 assert len(calls) == 0 45 46 assert task not in tasks 47 48 # Remove non existent item 49 with Spy.Spy(tasks, "indexSlow") as calls: 50 with pytest.raises(ValueError): 51 tasks.remove(task) 52 assert len(calls) == 0 53 54 self.checkSort(tasks) 55 56 def testRemoveAll(self): 57 tasks = WorkerTaskManager.WorkerTaskManager() 58 tasks_list = [] 59 for i in range(1000): 60 task = {"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i} 61 tasks.append(task) 62 tasks_list.append(task) 63 64 for task in tasks_list: 65 tasks.remove(task) 66 67 assert len(tasks.inner_paths) == 0 68 assert len(tasks) == 0 69 70 def testModify(self): 71 tasks = WorkerTaskManager.WorkerTaskManager() 72 for i in range(1000): 73 tasks.append({"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i}) 74 75 task = tasks[333] 76 task["priority"] += 10 77 78 with pytest.raises(AssertionError): 79 self.checkSort(tasks) 80 81 with Spy.Spy(tasks, "indexSlow") as calls: 82 tasks.updateItem(task) 83 assert len(calls) == 1 84 85 assert task in tasks 86 87 self.checkSort(tasks) 88 89 # Check reorder optimization 90 with Spy.Spy(tasks, "indexSlow") as calls: 91 tasks.updateItem(task, "priority", task["priority"] + 10) 92 assert len(calls) == 0 93 94 with Spy.Spy(tasks, "indexSlow") as calls: 95 tasks.updateItem(task, "priority", task["workers_num"] - 1) 96 assert len(calls) == 0 97 98 self.checkSort(tasks) 99 100 def testModifySamePriority(self): 101 tasks = WorkerTaskManager.WorkerTaskManager() 102 for i in range(1000): 103 tasks.append({"id": i, "priority": 10, "workers_num": 5, "inner_path": "file%s.json" % i}) 104 105 task = tasks[333] 106 107 # Check reorder optimization 108 with Spy.Spy(tasks, "indexSlow") as calls: 109 tasks.updateItem(task, "priority", task["workers_num"] - 1) 110 assert len(calls) == 0 111 112 def testIn(self): 113 tasks = WorkerTaskManager.WorkerTaskManager() 114 115 i = 1 116 task = {"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i} 117 118 assert task not in tasks 119 120 def testFindTask(self): 121 tasks = WorkerTaskManager.WorkerTaskManager() 122 for i in range(1000): 123 tasks.append({"id": i, "priority": i % 20, "workers_num": i % 3, "inner_path": "file%s.json" % i}) 124 125 assert tasks.findTask("file999.json") 126 assert not tasks.findTask("file-unknown.json") 127 tasks.remove(tasks.findTask("file999.json")) 128 assert not tasks.findTask("file999.json")