subscription_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 interpreter
  11. import (
  12. "bytes"
  13. "sync"
  14. "testing"
  15. "time"
  16. "devt.de/krotik/eliasdb/graph/data"
  17. )
  18. type testCallbackHandler struct {
  19. wg *sync.WaitGroup
  20. messages *bytes.Buffer
  21. finished bool
  22. }
  23. func (ch *testCallbackHandler) Publish(data map[string]interface{}, err error) {
  24. ch.messages.WriteString(formatData(data))
  25. ch.messages.WriteString("\n--\n")
  26. }
  27. func (ch *testCallbackHandler) IsFinished() bool {
  28. if ch.wg != nil {
  29. ch.wg.Done()
  30. }
  31. return ch.finished
  32. }
  33. func TestSubscription(t *testing.T) {
  34. gm, _ := songGraphGroups()
  35. if err := gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  36. "key": "StrangeSong1",
  37. "kind": "NewSong",
  38. "name": "bar",
  39. })); err != nil {
  40. t.Error(err)
  41. return
  42. }
  43. // Test fragments for different return types
  44. query := map[string]interface{}{
  45. "operationName": nil,
  46. "query": `
  47. subscription {
  48. Song(key : "StrangeSong1") {
  49. name
  50. }
  51. }
  52. `,
  53. "variables": nil,
  54. }
  55. cbh := &testCallbackHandler{&sync.WaitGroup{}, bytes.NewBufferString(""), false}
  56. cbh2 := &testCallbackHandler{nil, bytes.NewBufferString(""), false}
  57. res, err := runQuery("test", "main", query, gm, cbh, false)
  58. runQuery("test", "main", query, gm, cbh2, false)
  59. if f := formatData(res); f != `{
  60. "data": {
  61. "Song": [
  62. {
  63. "name": "StrangeSong1"
  64. }
  65. ]
  66. }
  67. }` || err != nil {
  68. t.Error("Unexpected result:", f, err)
  69. return
  70. }
  71. if cbh.messages.String() != `` {
  72. t.Error("Unexpected result:", cbh.messages.String())
  73. return
  74. }
  75. cbh.wg.Add(2)
  76. if err = gm.UpdateNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  77. "key": "StrangeSong1",
  78. "kind": "Song",
  79. "name": "foo",
  80. })); err != nil {
  81. t.Error(err)
  82. return
  83. }
  84. if err = gm.UpdateNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  85. "key": "StrangeSong1",
  86. "kind": "NewSong",
  87. "name": "bar",
  88. })); err != nil {
  89. t.Error(err)
  90. return
  91. }
  92. time.Sleep(5 * time.Millisecond)
  93. cbh.wg.Wait()
  94. if m := cbh.messages.String(); m != `{
  95. "data": {
  96. "Song": [
  97. {
  98. "name": "foo"
  99. }
  100. ]
  101. }
  102. }
  103. --
  104. ` {
  105. t.Error("Unexpected result:", m)
  106. return
  107. }
  108. cbh.wg.Add(1)
  109. cbh.finished = true
  110. if err = gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  111. "key": "StrangeSong1",
  112. "kind": "Song",
  113. "name": "bar",
  114. })); err != nil {
  115. t.Error(err)
  116. return
  117. }
  118. time.Sleep(5 * time.Millisecond)
  119. cbh.wg.Wait()
  120. if m := cbh.messages.String(); m != `{
  121. "data": {
  122. "Song": [
  123. {
  124. "name": "foo"
  125. }
  126. ]
  127. }
  128. }
  129. --
  130. {
  131. "data": {
  132. "Song": [
  133. {
  134. "name": "bar"
  135. }
  136. ]
  137. }
  138. }
  139. --
  140. ` {
  141. t.Error("Unexpected result:", m)
  142. return
  143. }
  144. // Ensure the subscription handler is gone
  145. if r := len(ruleMap); r != 1 {
  146. t.Error("Unexpected number of created rules:", r)
  147. return
  148. }
  149. for _, rule := range ruleMap {
  150. if r := len(rule.handlers); r != 1 {
  151. t.Error("Unexpected number of handlers:", r)
  152. }
  153. }
  154. // Reset rule Map
  155. ruleMap = make(map[string]*SystemRuleGraphQLSubscriptions)
  156. }