taskqueue_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package engine
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestTaskQueue(t *testing.T) {
  16. UnitTestResetIDs()
  17. // Create dummy processor
  18. proc := NewProcessor(1)
  19. // Create dummy event
  20. event := &Event{
  21. "DummyEvent",
  22. []string{"main"},
  23. nil,
  24. }
  25. // Create different root monitors with different IDs
  26. m1 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  27. // Create now different tasks which come from the different monitors
  28. t1 := &Task{proc, m1, event}
  29. tq := NewTaskQueue(proc.(*eventProcessor).messageQueue)
  30. tq.Push(t1)
  31. if res := tq.Size(); res != 1 {
  32. t.Error("Unexpected size:", res)
  33. return
  34. }
  35. tq.Clear()
  36. if res := tq.Size(); res != 0 {
  37. t.Error("Unexpected size:", res)
  38. return
  39. }
  40. if e := tq.Pop(); e != nil {
  41. t.Error("Unexpected event:", e)
  42. return
  43. }
  44. if res := tq.Size(); res != 0 {
  45. t.Error("Unexpected size:", res)
  46. return
  47. }
  48. testTaskQueuePushPop(t, tq, proc, event, t1)
  49. }
  50. func testTaskQueuePushPop(t *testing.T, tq *TaskQueue, proc Processor, event *Event, t1 *Task) {
  51. m2 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  52. m3 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  53. t2 := &Task{proc, m2, event}
  54. t3 := &Task{proc, m3, event}
  55. t4 := &Task{proc, m2.NewChildMonitor(5), event}
  56. t5 := &Task{proc, m2.NewChildMonitor(10), event}
  57. tq.Push(t1)
  58. tq.Push(t2)
  59. tq.Push(t3)
  60. tq.Push(t4)
  61. tq.Push(t5)
  62. if res := len(tq.queues); res != 3 {
  63. t.Error("Unexpected size:", res)
  64. return
  65. }
  66. if s := tq.queues[1].Size(); s != 1 {
  67. t.Error("Unexpected result:", s)
  68. return
  69. }
  70. if s := tq.queues[2].Size(); s != 3 {
  71. t.Error("Unexpected result:", s)
  72. return
  73. }
  74. if e := tq.Pop(); e != t1 && e != t2 && e != t3 {
  75. t.Error("Unexpected event:", e)
  76. return
  77. }
  78. if res := len(tq.queues); res != 3 {
  79. t.Error("Unexpected size:", res)
  80. return
  81. }
  82. tq.Pop()
  83. if res := len(tq.queues); res != 3 && res != 2 {
  84. t.Error("Unexpected size:", res)
  85. return
  86. }
  87. tq.Pop()
  88. if s := tq.Size(); s != 2 {
  89. t.Error("Unexpected result:", s)
  90. return
  91. }
  92. tq.Pop()
  93. tq.Pop()
  94. if s := tq.Size(); s != 0 {
  95. t.Error("Unexpected result:", s)
  96. return
  97. }
  98. if e := tq.Pop(); e != nil {
  99. t.Error("Unexpected event:", e)
  100. return
  101. }
  102. testTaskQueueMisc(t, tq, t5)
  103. }
  104. func testTaskQueueMisc(t *testing.T, tq *TaskQueue, t5 *Task) {
  105. tq.Push(t5)
  106. if fmt.Sprint(tq.queues) != "map[2:[ Task: RumbleProcessor 1 (workers:1) Monitor 5 (parent: Monitor 2 (parent: <nil> priority: 0 activated: false finished: false) priority: 10 activated: false finished: false) Event: DummyEvent main {} (10) ]]" {
  107. t.Error("Unexpected queue:", tq.queues)
  108. return
  109. }
  110. if e := tq.Pop(); e != t5 {
  111. t.Error("Unexpected event:", e)
  112. return
  113. }
  114. if fmt.Sprint(tq.queues) != "map[2:[ ]]" {
  115. t.Error("Unexpected list of ids:", tq.queues)
  116. return
  117. }
  118. if e := tq.Pop(); e != nil {
  119. t.Error("Unexpected event:", e)
  120. return
  121. }
  122. if fmt.Sprint(tq.queues) != "map[]" {
  123. t.Error("Unexpected list of ids:", tq.queues)
  124. return
  125. }
  126. if e := tq.Pop(); e != nil {
  127. t.Error("Unexpected event:", e)
  128. return
  129. }
  130. }
  131. func TestTaskQueueCorrectPriorities(t *testing.T) {
  132. UnitTestResetIDs()
  133. // Create dummy processor
  134. proc := NewProcessor(1)
  135. // Create dummy event
  136. event := &Event{
  137. "DummyEvent",
  138. []string{"main"},
  139. nil,
  140. }
  141. // Create different root monitors with different IDs
  142. m1 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  143. // Create now different tasks which come from the different monitors
  144. t1 := &Task{proc, m1, event}
  145. t2 := &Task{proc, m1.NewChildMonitor(5), event}
  146. t3 := &Task{proc, m1.NewChildMonitor(10), event}
  147. tq := NewTaskQueue(proc.(*eventProcessor).messageQueue)
  148. tq.Push(t2)
  149. tq.Push(t1)
  150. tq.Push(t3)
  151. if s := tq.Size(); s != 3 {
  152. t.Error("Unexpected result:", s)
  153. return
  154. }
  155. var popList []int
  156. popList = append(popList, tq.Pop().(*Task).m.Priority())
  157. popList = append(popList, tq.Pop().(*Task).m.Priority())
  158. popList = append(popList, tq.Pop().(*Task).m.Priority())
  159. if fmt.Sprint(popList) != "[0 5 10]" {
  160. t.Error("Unexpected poplist:", popList)
  161. return
  162. }
  163. }