vitess.io/vitess@v0.16.2/go/sync2/consolidator_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sync2 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestConsolidator(t *testing.T) { 25 con := NewConsolidator() 26 sql := "select * from SomeTable" 27 28 want := []ConsolidatorCacheItem{} 29 if !reflect.DeepEqual(con.Items(), want) { 30 t.Fatalf("expected consolidator to have no items") 31 } 32 33 orig, added := con.Create(sql) 34 if !added { 35 t.Fatalf("expected consolidator to register a new entry") 36 } 37 38 if !reflect.DeepEqual(con.Items(), want) { 39 t.Fatalf("expected consolidator to still have no items") 40 } 41 42 dup, added := con.Create(sql) 43 if added { 44 t.Fatalf("did not expect consolidator to register a new entry") 45 } 46 47 result := 1 48 go func() { 49 orig.Result = &result 50 orig.Broadcast() 51 }() 52 dup.Wait() 53 54 if *orig.Result.(*int) != result { 55 t.Errorf("failed to pass result") 56 } 57 if *orig.Result.(*int) != *dup.Result.(*int) { 58 t.Fatalf("failed to share the result") 59 } 60 61 want = []ConsolidatorCacheItem{{Query: sql, Count: 1}} 62 if !reflect.DeepEqual(con.Items(), want) { 63 t.Fatalf("expected consolidator to have one items %v", con.Items()) 64 } 65 66 // Running the query again should add a new entry since the original 67 // query execution completed 68 second, added := con.Create(sql) 69 if !added { 70 t.Fatalf("expected consolidator to register a new entry") 71 } 72 73 go func() { 74 second.Result = &result 75 second.Broadcast() 76 }() 77 dup.Wait() 78 79 want = []ConsolidatorCacheItem{{Query: sql, Count: 2}} 80 if !reflect.DeepEqual(con.Items(), want) { 81 t.Fatalf("expected consolidator to have two items %v", con.Items()) 82 } 83 84 }