taskqueue_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. m2 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  28. m3 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  29. // Create now different tasks which come from the different monitors
  30. t1 := &Task{proc, m1, event}
  31. t2 := &Task{proc, m2, event}
  32. t3 := &Task{proc, m3, event}
  33. t4 := &Task{proc, m2.NewChildMonitor(5), event}
  34. t5 := &Task{proc, m2.NewChildMonitor(10), event}
  35. tq := NewTaskQueue(proc.(*eventProcessor).messageQueue)
  36. tq.Push(t1)
  37. if res := tq.Size(); res != 1 {
  38. t.Error("Unexpected size:", res)
  39. return
  40. }
  41. tq.Clear()
  42. if res := tq.Size(); res != 0 {
  43. t.Error("Unexpected size:", res)
  44. return
  45. }
  46. if e := tq.Pop(); e != nil {
  47. t.Error("Unexpected event:", e)
  48. return
  49. }
  50. if res := tq.Size(); res != 0 {
  51. t.Error("Unexpected size:", res)
  52. return
  53. }
  54. tq.Push(t1)
  55. tq.Push(t2)
  56. tq.Push(t3)
  57. tq.Push(t4)
  58. tq.Push(t5)
  59. if res := len(tq.queues); res != 3 {
  60. t.Error("Unexpected size:", res)
  61. return
  62. }
  63. if s := tq.queues[1].Size(); s != 1 {
  64. t.Error("Unexpected result:", s)
  65. return
  66. }
  67. if s := tq.queues[2].Size(); s != 3 {
  68. t.Error("Unexpected result:", s)
  69. return
  70. }
  71. if e := tq.Pop(); e != t1 && e != t2 && e != t3 {
  72. t.Error("Unexpected event:", e)
  73. return
  74. }
  75. if res := len(tq.queues); res != 3 {
  76. t.Error("Unexpected size:", res)
  77. return
  78. }
  79. if e := tq.Pop(); e != t1 && e != t2 && e != t3 && e != t4 && e != t5 {
  80. t.Error("Unexpected event:", e)
  81. return
  82. }
  83. if res := len(tq.queues); res != 3 && res != 2 {
  84. t.Error("Unexpected size:", res)
  85. return
  86. }
  87. if e := tq.Pop(); e != t1 && e != t2 && e != t3 && e != t4 && e != t5 {
  88. t.Error("Unexpected event:", e)
  89. return
  90. }
  91. if s := tq.Size(); s != 2 {
  92. t.Error("Unexpected result:", s)
  93. return
  94. }
  95. if e := tq.Pop(); e != t1 && e != t2 && e != t3 && e != t4 && e != t5 {
  96. t.Error("Unexpected event:", e)
  97. return
  98. }
  99. if e := tq.Pop(); e != t1 && e != t2 && e != t3 && e != t4 && e != t5 {
  100. t.Error("Unexpected event:", e)
  101. return
  102. }
  103. if s := tq.Size(); s != 0 {
  104. t.Error("Unexpected result:", s)
  105. return
  106. }
  107. if e := tq.Pop(); e != nil {
  108. t.Error("Unexpected event:", e)
  109. return
  110. }
  111. tq.Push(t5)
  112. 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) ]]" {
  113. t.Error("Unexpected queue:", tq.queues)
  114. return
  115. }
  116. if e := tq.Pop(); e != t5 {
  117. t.Error("Unexpected event:", e)
  118. return
  119. }
  120. if fmt.Sprint(tq.queues) != "map[2:[ ]]" {
  121. t.Error("Unexpected list of ids:", tq.queues)
  122. return
  123. }
  124. if e := tq.Pop(); e != nil {
  125. t.Error("Unexpected event:", e)
  126. return
  127. }
  128. if fmt.Sprint(tq.queues) != "map[]" {
  129. t.Error("Unexpected list of ids:", tq.queues)
  130. return
  131. }
  132. if e := tq.Pop(); e != nil {
  133. t.Error("Unexpected event:", e)
  134. return
  135. }
  136. }
  137. func TestTaskQueueCorrectPriorities(t *testing.T) {
  138. UnitTestResetIDs()
  139. // Create dummy processor
  140. proc := NewProcessor(1)
  141. // Create dummy event
  142. event := &Event{
  143. "DummyEvent",
  144. []string{"main"},
  145. nil,
  146. }
  147. // Create different root monitors with different IDs
  148. m1 := newRootMonitor(nil, NewRuleScope(map[string]bool{"": true}), proc.(*eventProcessor).messageQueue)
  149. // Create now different tasks which come from the different monitors
  150. t1 := &Task{proc, m1, event}
  151. t2 := &Task{proc, m1.NewChildMonitor(5), event}
  152. t3 := &Task{proc, m1.NewChildMonitor(10), event}
  153. tq := NewTaskQueue(proc.(*eventProcessor).messageQueue)
  154. tq.Push(t2)
  155. tq.Push(t1)
  156. tq.Push(t3)
  157. if s := tq.Size(); s != 3 {
  158. t.Error("Unexpected result:", s)
  159. return
  160. }
  161. var popList []int
  162. popList = append(popList, tq.Pop().(*Task).m.Priority())
  163. popList = append(popList, tq.Pop().(*Task).m.Priority())
  164. popList = append(popList, tq.Pop().(*Task).m.Priority())
  165. if fmt.Sprint(popList) != "[0 5 10]" {
  166. t.Error("Unexpected poplist:", popList)
  167. return
  168. }
  169. }