github.com/dubbogo/gost@v1.14.0/container/gxsync/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 /* 18 * Licensed to the Apache Software Foundation (ASF) under one or more 19 * contributor license agreements. See the NOTICE file distributed with 20 * this work for additional information regarding copyright ownership. 21 * The ASF licenses this file to You under the Apache License, Version 2.0 22 * (the "License"); you may not use this file except in compliance with 23 * the License. You may obtain a copy of the License at 24 * 25 * http://www.apache.org/licenses/LICENSE-2.0 26 * 27 * Unless required by applicable law or agreed to in writing, software 28 * distributed under the License is distributed on an "AS IS" BASIS, 29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 30 * See the License for the specific language governing permissions and 31 * limitations under the License. 32 */ 33 34 package gxsync 35 36 import ( 37 "reflect" 38 "testing" 39 ) 40 41 func TestConsolidator(t *testing.T) { 42 con := NewConsolidator() 43 sql := "select * from SomeTable" 44 45 want := []ConsolidatorCacheItem{} 46 if !reflect.DeepEqual(con.Items(), want) { 47 t.Fatalf("expected consolidator to have no items") 48 } 49 50 orig, added := con.Create(sql) 51 if !added { 52 t.Fatalf("expected consolidator to register a new entry") 53 } 54 55 if !reflect.DeepEqual(con.Items(), want) { 56 t.Fatalf("expected consolidator to still have no items") 57 } 58 59 dup, added := con.Create(sql) 60 if added { 61 t.Fatalf("did not expect consolidator to register a new entry") 62 } 63 64 result := 1 65 go func() { 66 orig.Result = &result 67 orig.Broadcast() 68 }() 69 dup.Wait() 70 71 if *orig.Result.(*int) != result { 72 t.Errorf("failed to pass result") 73 } 74 if *orig.Result.(*int) != *dup.Result.(*int) { 75 t.Fatalf("failed to share the result") 76 } 77 78 want = []ConsolidatorCacheItem{{Query: sql, Count: 1}} 79 if !reflect.DeepEqual(con.Items(), want) { 80 t.Fatalf("expected consolidator to have one items %v", con.Items()) 81 } 82 83 // Running the query again should add a new entry since the original 84 // query execution completed 85 second, added := con.Create(sql) 86 if !added { 87 t.Fatalf("expected consolidator to register a new entry") 88 } 89 90 go func() { 91 second.Result = &result 92 second.Broadcast() 93 }() 94 dup.Wait() 95 96 want = []ConsolidatorCacheItem{{Query: sql, Count: 2}} 97 if !reflect.DeepEqual(con.Items(), want) { 98 t.Fatalf("expected consolidator to have two items %v", con.Items()) 99 } 100 101 }