table_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lookup
  15. import (
  16. "sync"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. func TestTable(t *testing.T) {
  21. var wg sync.WaitGroup
  22. wg.Add(2)
  23. err := CreateInstance("test1", "memory", &ast.Options{
  24. DATASOURCE: "test",
  25. TYPE: "memory",
  26. KIND: "lookup",
  27. KEY: "id",
  28. })
  29. if err != nil {
  30. t.Error(err)
  31. return
  32. }
  33. err = CreateInstance("test2", "memory", &ast.Options{
  34. DATASOURCE: "test2",
  35. TYPE: "memory",
  36. KIND: "lookup",
  37. KEY: "id",
  38. })
  39. if err != nil {
  40. t.Error(err)
  41. return
  42. }
  43. go func() {
  44. for i := 0; i < 3; i++ {
  45. _, err := Attach("test1")
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. _, err = Attach("test2")
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. }
  54. wg.Done()
  55. }()
  56. go func() {
  57. for i := 0; i < 3; i++ {
  58. _, err := Attach("test2")
  59. if err != nil {
  60. t.Error(err)
  61. }
  62. _, err = Attach("test1")
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. }
  67. wg.Done()
  68. }()
  69. wg.Wait()
  70. if len(instances) != 2 {
  71. t.Errorf("expect 2 instances, but got %d", len(instances))
  72. return
  73. }
  74. for _, ins := range instances {
  75. if ins.count != 6 {
  76. t.Errorf("expect 6 count, but got %d", ins.count)
  77. return
  78. }
  79. }
  80. err = DropInstance("test1")
  81. if err == nil {
  82. t.Error("should have error to drop instance")
  83. return
  84. }
  85. for i := 0; i < 6; i++ {
  86. Detach("test1")
  87. }
  88. err = DropInstance("test1")
  89. if err != nil {
  90. t.Error(err)
  91. return
  92. }
  93. if len(instances) != 1 {
  94. t.Errorf("expect 2 instances, but got %d", len(instances))
  95. return
  96. }
  97. }