eventsource_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package brawler
  11. import (
  12. "bytes"
  13. "fmt"
  14. "testing"
  15. "devt.de/krotik/eliasdb/graph"
  16. "devt.de/krotik/eliasdb/graph/data"
  17. "devt.de/krotik/eliasdb/graph/graphstorage"
  18. )
  19. type mockEventPublisher struct {
  20. buf bytes.Buffer
  21. err error
  22. }
  23. func (p *mockEventPublisher) AddEvent(name string, kind []string, state map[interface{}]interface{}) error {
  24. if p.err == nil {
  25. p.buf.WriteString(fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v", name, kind, state["part"], state["node"], state["edge"],
  26. state["old_node"], state["old_edge"]))
  27. }
  28. return p.err
  29. }
  30. func TestEventSource(t *testing.T) {
  31. ep := &mockEventPublisher{bytes.Buffer{}, nil}
  32. log := bytes.Buffer{}
  33. mgs := graphstorage.NewMemoryGraphStorage("iterator test")
  34. gm := graph.NewGraphManager(mgs)
  35. AddEventPublisher(gm, ep, &log)
  36. if res := fmt.Sprint(gm.GraphRules()); res != "[brawler.eventbridge system.deletenodeedges system.updatenodestats]" {
  37. t.Error("Unexpected result:", res)
  38. return
  39. }
  40. if res := fmt.Sprint(ep.buf.String()); res != `
  41. `[1:] {
  42. t.Error("Unexpected result:", res)
  43. return
  44. }
  45. // Now generate some events
  46. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  47. "key": "foo",
  48. "kind": "bar",
  49. }))
  50. if res := fmt.Sprint(ep.buf.String()); res != `
  51. db.node.created-[db.node.created]-main-GraphNode:
  52. key : foo
  53. kind : bar
  54. -<nil>-<nil>-<nil>`[1:] {
  55. t.Error("Unexpected result:", res)
  56. return
  57. }
  58. ep.err = fmt.Errorf("foo")
  59. gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  60. "key": "foo",
  61. "kind": "bar",
  62. }))
  63. if log.String() != "foo" {
  64. t.Error("Expected some errors:", log.String())
  65. return
  66. }
  67. ep.err = nil
  68. node1 := data.NewGraphNode()
  69. node1.SetAttr("key", "123")
  70. node1.SetAttr("kind", "mykind")
  71. node1.SetAttr("Name", "Node1")
  72. gm.StoreNode("main", node1)
  73. node2 := data.NewGraphNode()
  74. node2.SetAttr("key", "456")
  75. node2.SetAttr("kind", "mykind")
  76. node2.SetAttr("Name", "Node2")
  77. gm.StoreNode("main", node2)
  78. node3 := data.NewGraphNode()
  79. node3.SetAttr("key", "789")
  80. node3.SetAttr("kind", "mykind")
  81. node3.SetAttr("Name", "Node3")
  82. gm.StoreNode("main", node3)
  83. edge := data.NewGraphEdge()
  84. edge.SetAttr("key", "abc")
  85. edge.SetAttr("kind", "myedge")
  86. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  87. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  88. edge.SetAttr(data.EdgeEnd1Role, "node1")
  89. edge.SetAttr(data.EdgeEnd1Cascading, true)
  90. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  91. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  92. edge.SetAttr(data.EdgeEnd2Role, "node2")
  93. edge.SetAttr(data.EdgeEnd2Cascading, false)
  94. gm.StoreEdge("main", edge)
  95. if res := fmt.Sprint(ep.buf.String()); res != `
  96. db.node.created-[db.node.created]-main-GraphNode:
  97. key : foo
  98. kind : bar
  99. -<nil>-<nil>-<nil>db.node.created-[db.node.created]-main-GraphNode:
  100. key : 123
  101. kind : mykind
  102. Name : Node1
  103. -<nil>-<nil>-<nil>db.node.created-[db.node.created]-main-GraphNode:
  104. key : 456
  105. kind : mykind
  106. Name : Node2
  107. -<nil>-<nil>-<nil>db.node.created-[db.node.created]-main-GraphNode:
  108. key : 789
  109. kind : mykind
  110. Name : Node3
  111. -<nil>-<nil>-<nil>db.edge.created-[db.edge.created]-main-<nil>-GraphEdge:
  112. key : abc
  113. kind : myedge
  114. end1cascading : true
  115. end1key : 123
  116. end1kind : mykind
  117. end1role : node1
  118. end2cascading : false
  119. end2key : 456
  120. end2kind : mykind
  121. end2role : node2
  122. -<nil>-<nil>`[1:] {
  123. t.Error("Unexpected result:", res)
  124. return
  125. }
  126. ep.buf.Reset()
  127. // Do some updates
  128. edge = data.NewGraphEdge()
  129. edge.SetAttr("key", "abc")
  130. edge.SetAttr("kind", "myedge")
  131. edge.SetAttr("foo", "bar")
  132. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  133. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  134. edge.SetAttr(data.EdgeEnd1Role, "node1")
  135. edge.SetAttr(data.EdgeEnd1Cascading, true)
  136. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  137. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  138. edge.SetAttr(data.EdgeEnd2Role, "node2")
  139. edge.SetAttr(data.EdgeEnd2Cascading, false)
  140. gm.StoreEdge("main", edge)
  141. node1 = data.NewGraphNode()
  142. node1.SetAttr("key", "123")
  143. node1.SetAttr("kind", "mykind")
  144. node1.SetAttr("Name", "Node66")
  145. gm.StoreNode("main", node1)
  146. if res := fmt.Sprint(ep.buf.String()); res != `
  147. db.edge.updated-[db.edge.updated]-main-<nil>-GraphEdge:
  148. key : abc
  149. kind : myedge
  150. end1cascading : true
  151. end1key : 123
  152. end1kind : mykind
  153. end1role : node1
  154. end2cascading : false
  155. end2key : 456
  156. end2kind : mykind
  157. end2role : node2
  158. foo : bar
  159. -<nil>-GraphEdge:
  160. key : abc
  161. kind : myedge
  162. end1cascading : true
  163. end1key : 123
  164. end1kind : mykind
  165. end1role : node1
  166. end2cascading : false
  167. end2key : 456
  168. end2kind : mykind
  169. end2role : node2
  170. db.node.updated-[db.node.updated]-main-GraphNode:
  171. key : 123
  172. kind : mykind
  173. Name : Node66
  174. -<nil>-GraphNode:
  175. key : 123
  176. kind : mykind
  177. Name : Node1
  178. -<nil>`[1:] {
  179. t.Error("Unexpected result:", res)
  180. return
  181. }
  182. ep.buf.Reset()
  183. // Do deletions
  184. gm.RemoveNode("main", "456", "mykind") // This should also delete the edge
  185. if res := fmt.Sprint(ep.buf.String()); res != `
  186. db.node.deleted-[db.node.deleted]-main-GraphNode:
  187. key : 456
  188. kind : mykind
  189. Name : Node2
  190. -<nil>-<nil>-<nil>db.edge.deleted-[db.edge.deleted]-main-<nil>-GraphEdge:
  191. key : abc
  192. kind : myedge
  193. end1cascading : true
  194. end1key : 123
  195. end1kind : mykind
  196. end1role : node1
  197. end2cascading : false
  198. end2key : 456
  199. end2kind : mykind
  200. end2role : node2
  201. foo : bar
  202. -<nil>-<nil>`[1:] {
  203. t.Error("Unexpected result:", res)
  204. return
  205. }
  206. }