|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/staticbackendhq/core/config" |
| 6 | + "github.com/staticbackendhq/core/internal" |
| 7 | + "github.com/staticbackendhq/core/logger" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + redisCache *Cache |
| 15 | + devCache *CacheDev |
| 16 | + adminToken internal.Token |
| 17 | + adminAuth internal.Auth |
| 18 | + document string |
| 19 | +) |
| 20 | + |
| 21 | +type suite struct { |
| 22 | + name string |
| 23 | + cache internal.Volatilizer |
| 24 | +} |
| 25 | + |
| 26 | +func TestMain(m *testing.M) { |
| 27 | + config.Current = config.LoadConfig() |
| 28 | + logz := logger.Get(config.Current) |
| 29 | + redisCache = NewCache(logz) |
| 30 | + devCache = NewDevCache(logz) |
| 31 | + |
| 32 | + adminAuth = internal.Auth{ |
| 33 | + AccountID: "047cfe5b-b91d-4ec6-9bc2-8f68309d8532", |
| 34 | + UserID: "5dc37900-2a2e-46d9-8a5d-6699376975ad", |
| 35 | + |
| 36 | + Role: 100, |
| 37 | + Token: adminToken.Token, |
| 38 | + } |
| 39 | + document = `{"accountId":"047cfe5b-b91d-4ec6-9bc2-8f68309d8532","created":"2022-08-31T19:07:36.296787226+03:00","done":true,"id":"872c8192-c610-4fc5-bc8e-22c01f01c798","likes":0,"title":"updated","todos":[{"done":true,"title":"updated"},{"done":false,"title":"sub2"}]}` |
| 40 | + os.Exit(m.Run()) |
| 41 | +} |
| 42 | + |
| 43 | +func TestCacheSubscribe(t *testing.T) { |
| 44 | + tests := []suite{ |
| 45 | + {name: "subscribe with redis cache", cache: redisCache}, |
| 46 | + {name: "subscribe with dev mem cache", cache: devCache}, |
| 47 | + } |
| 48 | + for _, tc := range tests { |
| 49 | + t.Run(tc.name, func(t *testing.T) { |
| 50 | + receiver := make(chan internal.Command) |
| 51 | + closeCn := make(chan bool) |
| 52 | + defer close(receiver) |
| 53 | + defer close(closeCn) |
| 54 | + |
| 55 | + payload := internal.Command{Type: internal.MsgTypeError, Data: "invalid token", Channel: "random_cahn"} |
| 56 | + |
| 57 | + go tc.cache.Subscribe(receiver, "", payload.Channel, closeCn) |
| 58 | + time.Sleep(10 * time.Millisecond) // need to wait for proper subscriber startup |
| 59 | + |
| 60 | + err := tc.cache.Publish(payload) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err.Error()) |
| 63 | + } |
| 64 | + timer := time.NewTimer(5 * time.Second) |
| 65 | + select { |
| 66 | + case res := <-receiver: |
| 67 | + if res != payload { |
| 68 | + t.Error("Incorrect message is received") |
| 69 | + } |
| 70 | + break |
| 71 | + case <-timer.C: |
| 72 | + t.Fatal("The channel does not received a message") |
| 73 | + |
| 74 | + } |
| 75 | + closeCn <- true |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestCacheSubscribeOnDBEvent(t *testing.T) { |
| 81 | + tests := []suite{ |
| 82 | + {name: "receive db event with redis cache", cache: redisCache}, |
| 83 | + {name: "receive db event with dev mem cache", cache: devCache}, |
| 84 | + } |
| 85 | + for _, tc := range tests { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + receiver := make(chan internal.Command) |
| 88 | + closeCn := make(chan bool) |
| 89 | + defer close(receiver) |
| 90 | + defer close(closeCn) |
| 91 | + |
| 92 | + err := tc.cache.SetTyped("token", adminAuth) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err.Error()) |
| 95 | + } |
| 96 | + |
| 97 | + payload := internal.Command{Type: internal.MsgTypeDBUpdated, Data: document, Channel: "random_cahn"} |
| 98 | + |
| 99 | + go tc.cache.Subscribe(receiver, "token", payload.Channel, closeCn) |
| 100 | + |
| 101 | + time.Sleep(10 * time.Millisecond) // need to wait for proper subscriber startup |
| 102 | + |
| 103 | + err = tc.cache.Publish(payload) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err.Error()) |
| 106 | + } |
| 107 | + timer := time.NewTimer(5 * time.Second) |
| 108 | + defer timer.Stop() |
| 109 | + select { |
| 110 | + case res := <-receiver: |
| 111 | + if res != payload { |
| 112 | + t.Error("Incorrect message is received") |
| 113 | + } |
| 114 | + break |
| 115 | + case <-timer.C: |
| 116 | + t.Fatal("The channel does not received a message") |
| 117 | + |
| 118 | + } |
| 119 | + closeCn <- true |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestCacheDontReceiveDBEvent(t *testing.T) { |
| 125 | + tests := []suite{ |
| 126 | + { |
| 127 | + name: "DB event with incorrect token is not send to subscriber with redis cache", |
| 128 | + cache: redisCache, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "DB event with incorrect token is not send to subscriber with dev cache", |
| 132 | + cache: devCache, |
| 133 | + }, |
| 134 | + } |
| 135 | + for _, tc := range tests { |
| 136 | + t.Run(tc.name, func(t *testing.T) { |
| 137 | + receiver := make(chan internal.Command) |
| 138 | + closeCn := make(chan bool) |
| 139 | + defer close(receiver) |
| 140 | + defer close(closeCn) |
| 141 | + |
| 142 | + wrongAuth := adminAuth |
| 143 | + wrongAuth.AccountID = "wrongId" |
| 144 | + err := tc.cache.SetTyped("token", wrongAuth) |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err.Error()) |
| 147 | + } |
| 148 | + event := internal.Command{Type: internal.MsgTypeDBUpdated, Data: document, Channel: "chan"} |
| 149 | + go tc.cache.Subscribe(receiver, "token", event.Channel, closeCn) |
| 150 | + |
| 151 | + time.Sleep(10 * time.Millisecond) // need to wait for proper subscriber startup |
| 152 | + |
| 153 | + err = tc.cache.Publish(event) |
| 154 | + if err != nil { |
| 155 | + t.Fatal(err.Error()) |
| 156 | + } |
| 157 | + timer := time.NewTimer(2 * time.Second) |
| 158 | + defer timer.Stop() |
| 159 | + select { |
| 160 | + case res := <-receiver: |
| 161 | + closeCn <- true |
| 162 | + t.Fatalf("The message should not be received\nReceived: %#v", res) |
| 163 | + case <-timer.C: |
| 164 | + closeCn <- true |
| 165 | + |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestCachePublishDocument(t *testing.T) { |
| 172 | + tests := []suite{ |
| 173 | + {name: "receive db event with redis cache", cache: redisCache}, |
| 174 | + {name: "receive db event with dev mem cache", cache: devCache}, |
| 175 | + } |
| 176 | + for _, tc := range tests { |
| 177 | + t.Run(tc.name, func(t *testing.T) { |
| 178 | + receiver := make(chan internal.Command) |
| 179 | + closeCn := make(chan bool) |
| 180 | + defer close(receiver) |
| 181 | + defer close(closeCn) |
| 182 | + |
| 183 | + err := tc.cache.SetTyped("token", adminAuth) |
| 184 | + if err != nil { |
| 185 | + t.Fatal(err.Error()) |
| 186 | + } |
| 187 | + |
| 188 | + payload := internal.Command{Type: internal.MsgTypeDBUpdated, Data: document, Channel: "random_cahn"} |
| 189 | + |
| 190 | + // convert to map for simulation of real usage |
| 191 | + var documentMap map[string]interface{} |
| 192 | + if err := json.Unmarshal([]byte(document), &documentMap); err != nil { |
| 193 | + t.Fatal(err.Error()) |
| 194 | + } |
| 195 | + |
| 196 | + go tc.cache.Subscribe(receiver, "token", payload.Channel, closeCn) |
| 197 | + |
| 198 | + time.Sleep(10 * time.Millisecond) // need to wait for proper subscriber startup |
| 199 | + |
| 200 | + tc.cache.PublishDocument(payload.Channel, payload.Type, documentMap) |
| 201 | + timer := time.NewTimer(5 * time.Second) |
| 202 | + defer timer.Stop() |
| 203 | + select { |
| 204 | + case res := <-receiver: |
| 205 | + if res != payload { |
| 206 | + t.Error("Incorrect message is received") |
| 207 | + } |
| 208 | + break |
| 209 | + case <-timer.C: |
| 210 | + t.Fatal("The channel does not received a message") |
| 211 | + |
| 212 | + } |
| 213 | + closeCn <- true |
| 214 | + }) |
| 215 | + } |
| 216 | +} |
0 commit comments