github.com/hata/goseq@v0.0.0-20150316021154-a5ca66a92399/taskmanager_test.go (about) 1 package goseq 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 ) 8 9 const ( 10 defaultIndexSize = 1024 11 ) 12 13 func TestNewTaskManager(t *testing.T) { 14 tm := NewTaskManager(defaultIndexSize) 15 if tm == nil { 16 t.Error("NewTaskManager() failed.") 17 } 18 } 19 20 func TestNewTaskManagerStruct(t *testing.T) { 21 tm := newTaskManager(defaultIndexSize) 22 if tm.seqToIndexFunc == nil { 23 t.Error("seqToIndexFunc should be initialized.") 24 } 25 } 26 27 func TestAddHandler(t *testing.T) { 28 tm := newTaskManager(defaultIndexSize) 29 f := func(id SequenceID, index int) {} 30 tm.AddHandler(f) 31 if len(tm.handlerGroups) != 1 || tm.handlerGroups[0].numOfHandlers() != 1 { 32 t.Error("AddHandler failed to add a new task. len(ct.handlerGroups):", len(tm.handlerGroups)) 33 } 34 tm.AddHandler(f, f) 35 if len(tm.handlerGroups) != 2 || tm.handlerGroups[1].numOfHandlers() != 2 { 36 t.Error("AddHandler failed to add a new task. len(ct.handlerGroups):", len(tm.handlerGroups)) 37 } 38 } 39 40 func TestAddHandlerForSomeHandlers(t *testing.T) { 41 tm := newTaskManager(defaultIndexSize) 42 f := func(id SequenceID, index int) {} 43 for i := 0; i < 10; i++ { 44 tm.AddHandler(f) 45 } 46 if len(tm.handlerGroups) != 10 { 47 t.Error("AddHandler cannot add several tasks.", len(tm.handlerGroups)) 48 } 49 } 50 51 func TestAddHandlers(t *testing.T) { 52 tm := newTaskManager(defaultIndexSize) 53 f := func(id SequenceID, index int) {} 54 handlers := make([]TaskHandler, 10, 10) 55 for i := 0; i < 10; i++ { 56 handlers[i] = f 57 } 58 tm.AddHandlers(handlers) 59 if len(tm.handlerGroups) != 1 || tm.handlerGroups[0].numOfHandlers() != 10 { 60 t.Error("AddHandler cannot add several tasks to a group.", len(tm.handlerGroups)) 61 } 62 } 63 64 func TestPut(t *testing.T) { 65 tm := newTaskManager(2) 66 value := -1 67 currentID := -1 68 f := func(id SequenceID, index int) { 69 currentID = int(id) 70 value = index 71 } 72 tm.Put(f) 73 if value != 0 || currentID != 0 { 74 t.Error("Put should call initialier and set index.") 75 } 76 tm.Put(f) 77 if value != 1 || currentID != 1 { 78 t.Error("Put should call initializer and set a new index.") 79 } 80 tm.Put(f) 81 if value != 0 || currentID != 2 { 82 t.Error("handler's index should be less than size.") 83 } 84 } 85 86 func TestBasicBehavior(t *testing.T) { 87 count := 0 88 f := func(id SequenceID, index int) { 89 count++ 90 } 91 92 tm := NewTaskManager(defaultIndexSize) 93 tm.AddHandler(f) 94 tm.Start() 95 tm.Put(nil) 96 tm.Stop() 97 if count != 1 { 98 t.Error("TaskManager behavior has a problem. count:", count) 99 } 100 } 101 102 func TestSomeHandlersBehavior(t *testing.T) { 103 var m sync.Mutex 104 count := 0 105 f := func(id SequenceID, index int) { 106 m.Lock() 107 defer m.Unlock() 108 count++ 109 } 110 111 tm := NewTaskManager(defaultIndexSize) 112 tm.AddHandler(f) 113 tm.AddHandler(f) 114 tm.AddHandler(f) 115 tm.AddHandler(f) 116 tm.AddHandler(f) 117 tm.Start() 118 tm.Put(nil) 119 tm.Put(nil) 120 tm.Stop() 121 122 if count != 10 { 123 t.Error("TaskManager behavior has a problem. count:", count) 124 } 125 } 126 127 func TestWaitingPut(t *testing.T) { 128 values := make([]int, 4, 4) 129 handler := func(id SequenceID, index int) { 130 time.Sleep(10) 131 values[index] += 1 132 } 133 134 tm := NewTaskManager(4) 135 tm.AddHandler(handler) 136 tm.Start() 137 for i := 0; i < 10; i++ { 138 tm.Put(func(id SequenceID, index int) { 139 values[index] = 0 140 }) 141 } 142 tm.Stop() 143 144 for _, v := range values { 145 if v != 1 { 146 t.Error("Value should be accessed from 1 handler at the same time.", v) 147 } 148 } 149 } 150 151 func BenchmarkHandler(b *testing.B) { 152 f := func(id SequenceID, index int) { 153 index++ 154 } 155 156 tm := NewTaskManager(defaultIndexSize) 157 tm.AddHandler(f) 158 tm.Start() 159 b.ResetTimer() 160 for i := 0; i < 100000; i++ { 161 tm.Put(nil) 162 } 163 tm.Stop() 164 } 165 166 func BenchmarkPut(b *testing.B) { 167 count := 100000 168 values := make([]int, defaultIndexSize) 169 handler := func(id SequenceID, index int) { 170 values[index] += 1 171 } 172 tm := NewTaskManager(defaultIndexSize) 173 tm.AddHandler(handler).Then(handler).Then(handler) 174 tm.Start() 175 b.ResetTimer() 176 for i := 0; i < count; i++ { 177 tm.Put(func(id SequenceID, index int) { 178 values[index] = 0 179 }) 180 } 181 tm.Stop() 182 for _, value := range values { 183 if value != 3 { 184 b.Error("Error result is incorrect. value:", value) 185 } 186 } 187 }