github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/stmtctx/id_range_test.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stmtctx 16 17 import ( 18 "sync/atomic" 19 "testing" 20 21 "github.com/sourcegraph/conc" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestIDRange_Next(t *testing.T) { 26 rang := NewIDRange(0, 3) 27 28 assert := assert.New(t) 29 30 for i := 0; i < 3; i++ { 31 id, err := rang.Next() 32 assert.Nil(err) 33 assert.Equal(int64(i+1), id) 34 } 35 36 _, err := rang.Next() 37 assert.NotNil(err) 38 } 39 40 func TestIDRange_NextParallel(t *testing.T) { 41 const n = 10000000 42 rang := NewIDRange(0, n) 43 44 const p = 20 45 46 wg := conc.WaitGroup{} 47 total := atomic.Int64{} 48 for i := 0; i < p; i++ { 49 wg.Go(func() { 50 var subn int64 51 for { 52 _, err := rang.Next() 53 if err != nil { 54 total.Add(subn) 55 return 56 } 57 subn++ 58 } 59 }) 60 } 61 62 wg.Wait() 63 assert.Equal(t, total.Load(), int64(n)) 64 }