vitess.io/vitess@v0.16.2/go/pools/id_pool_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 pools 18 19 import ( 20 "reflect" 21 "strings" 22 "testing" 23 ) 24 25 func (pool *IDPool) want(want *IDPool, t *testing.T) { 26 if pool.maxUsed != want.maxUsed { 27 t.Errorf("pool.maxUsed = %#v, want %#v", pool.maxUsed, want.maxUsed) 28 } 29 30 if !reflect.DeepEqual(pool.used, want.used) { 31 t.Errorf("pool.used = %#v, want %#v", pool.used, want.used) 32 } 33 } 34 35 func TestIDPoolFirstGet(t *testing.T) { 36 pool := NewIDPool(0) 37 38 if got := pool.Get(); got != 1 { 39 t.Errorf("pool.Get() = %v, want 1", got) 40 } 41 42 pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 1}, t) 43 } 44 45 func TestIDPoolSecondGet(t *testing.T) { 46 pool := NewIDPool(0) 47 pool.Get() 48 49 if got := pool.Get(); got != 2 { 50 t.Errorf("pool.Get() = %v, want 2", got) 51 } 52 53 pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 2}, t) 54 } 55 56 func TestIDPoolPutToUsedSet(t *testing.T) { 57 pool := NewIDPool(0) 58 id1 := pool.Get() 59 pool.Get() 60 pool.Put(id1) 61 62 pool.want(&IDPool{used: map[uint32]bool{1: true}, maxUsed: 2}, t) 63 } 64 65 func TestIDPoolPutMaxUsed1(t *testing.T) { 66 pool := NewIDPool(0) 67 id1 := pool.Get() 68 pool.Put(id1) 69 70 pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 0}, t) 71 } 72 73 func TestIDPoolPutMaxUsed2(t *testing.T) { 74 pool := NewIDPool(0) 75 pool.Get() 76 id2 := pool.Get() 77 pool.Put(id2) 78 79 pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 1}, t) 80 } 81 82 func TestIDPoolGetFromUsedSet(t *testing.T) { 83 pool := NewIDPool(0) 84 id1 := pool.Get() 85 pool.Get() 86 pool.Put(id1) 87 88 if got := pool.Get(); got != 1 { 89 t.Errorf("pool.Get() = %v, want 1", got) 90 } 91 92 pool.want(&IDPool{used: map[uint32]bool{}, maxUsed: 2}, t) 93 } 94 95 func wantError(want string, t *testing.T) { 96 rec := recover() 97 if rec == nil { 98 t.Errorf("expected panic, but there wasn't one") 99 } 100 err, ok := rec.(error) 101 if !ok || !strings.Contains(err.Error(), want) { 102 t.Errorf("wrong error, got '%v', want '%v'", err, want) 103 } 104 } 105 106 func TestIDPoolPut0(t *testing.T) { 107 pool := NewIDPool(0) 108 pool.Get() 109 110 defer wantError("invalid value", t) 111 pool.Put(0) 112 } 113 114 func TestIDPoolPutInvalid(t *testing.T) { 115 pool := NewIDPool(0) 116 pool.Get() 117 118 defer wantError("invalid value", t) 119 pool.Put(5) 120 } 121 122 func TestIDPoolPutDuplicate(t *testing.T) { 123 pool := NewIDPool(0) 124 pool.Get() 125 pool.Get() 126 pool.Put(1) 127 128 defer wantError("already recycled", t) 129 pool.Put(1) 130 }