github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/concurrent_map_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package interlock 15 16 import ( 17 "sync" 18 19 . "github.com/whtcorpsinc/check" 20 "github.com/whtcorpsinc/milevadb/soliton/chunk" 21 ) 22 23 // TestConcurrentMap first inserts 1000 entries, then checks them 24 func (cm *pkgTestSuite) TestConcurrentMap(c *C) { 25 m := newConcurrentMap() 26 const iterations = 1000 27 const mod = 111 28 wg := &sync.WaitGroup{} 29 wg.Add(2) 30 // Using go routines insert 1000 entires into the map. 31 go func() { 32 defer wg.Done() 33 for i := 0; i < iterations/2; i++ { 34 // Add entry to map. 35 m.Insert(uint64(i%mod), &entry{chunk.EventPtr{ChkIdx: uint32(i), EventIdx: uint32(i)}, nil}) 36 } 37 }() 38 39 go func() { 40 defer wg.Done() 41 for i := iterations / 2; i < iterations; i++ { 42 // Add entry to map. 43 m.Insert(uint64(i%mod), &entry{chunk.EventPtr{ChkIdx: uint32(i), EventIdx: uint32(i)}, nil}) 44 } 45 }() 46 wg.Wait() 47 48 // check whether i exist in the map, surely 49 for i := 0; i < iterations; i++ { 50 found := false 51 for en, ok := m.Get(uint64(i % mod)); en != nil; en = en.next { 52 c.Assert(ok, IsTrue) 53 if en.ptr.EventIdx == uint32(i) && en.ptr.ChkIdx == uint32(i) { 54 found = true 55 } 56 } 57 c.Assert(found, IsTrue) 58 } 59 // test some unexpected cases 60 _, ok := m.Get(uint64(mod)) 61 c.Assert(ok, IsFalse) 62 63 _, ok = m.Get(uint64(mod + 1)) 64 c.Assert(ok, IsFalse) 65 }