github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/events/events_test.go (about) 1 package events 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "reflect" 8 "testing" 9 10 "github.com/prebid/prebid-server/v2/stored_requests" 11 "github.com/prebid/prebid-server/v2/stored_requests/caches/memory" 12 ) 13 14 func TestListen(t *testing.T) { 15 ep := &fakeProducer{ 16 saves: make(chan Save), 17 invalidations: make(chan Invalidation), 18 } 19 cache := stored_requests.Cache{ 20 Requests: memory.NewCache(256*1024, -1, "Requests"), 21 Imps: memory.NewCache(256*1024, -1, "Imps"), 22 Responses: memory.NewCache(256*1024, -1, "Responses"), 23 Accounts: memory.NewCache(256*1024, -1, "Account"), 24 } 25 26 // create channels to synchronize 27 saveOccurred := make(chan struct{}) 28 invalidateOccurred := make(chan struct{}) 29 listener := NewEventListener( 30 func() { saveOccurred <- struct{}{} }, 31 func() { invalidateOccurred <- struct{}{} }, 32 ) 33 34 go listener.Listen(cache, ep) 35 defer listener.Stop() 36 37 id := "1" 38 idSlice := []string{id} 39 config := fmt.Sprintf(`{"id": "%s"}`, id) 40 data := map[string]json.RawMessage{id: json.RawMessage(config)} 41 save := Save{ 42 Requests: data, 43 Imps: data, 44 Responses: data, 45 Accounts: data, 46 } 47 cache.Requests.Save(context.Background(), save.Requests) 48 cache.Imps.Save(context.Background(), save.Imps) 49 cache.Accounts.Save(context.Background(), save.Accounts) 50 51 config = fmt.Sprintf(`{"id": "%s", "updated": true}`, id) 52 data = map[string]json.RawMessage{id: json.RawMessage(config)} 53 save = Save{ 54 Requests: data, 55 Imps: data, 56 Responses: data, 57 Accounts: data, 58 } 59 60 ep.saves <- save 61 <-saveOccurred 62 63 requestData := cache.Requests.Get(context.Background(), idSlice) 64 impData := cache.Imps.Get(context.Background(), idSlice) 65 respData := cache.Responses.Get(context.Background(), idSlice) 66 accountData := cache.Accounts.Get(context.Background(), idSlice) 67 if !reflect.DeepEqual(requestData, data) || !reflect.DeepEqual(impData, data) || !reflect.DeepEqual(respData, data) || !reflect.DeepEqual(accountData, data) { 68 t.Error("Update failed") 69 } 70 71 invalidation := Invalidation{ 72 Requests: idSlice, 73 Imps: idSlice, 74 Responses: idSlice, 75 Accounts: idSlice, 76 } 77 78 ep.invalidations <- invalidation 79 <-invalidateOccurred 80 81 requestData = cache.Requests.Get(context.Background(), idSlice) 82 impData = cache.Imps.Get(context.Background(), idSlice) 83 respData = cache.Responses.Get(context.Background(), idSlice) 84 accountData = cache.Accounts.Get(context.Background(), idSlice) 85 if len(requestData) > 0 || len(impData) > 0 || len(respData) > 0 || len(accountData) > 0 { 86 t.Error("Invalidate failed") 87 } 88 } 89 90 type fakeProducer struct { 91 saves chan Save 92 invalidations chan Invalidation 93 } 94 95 func (p *fakeProducer) Saves() <-chan Save { 96 return p.saves 97 } 98 99 func (p *fakeProducer) Invalidations() <-chan Invalidation { 100 return p.invalidations 101 }