github.com/dubbogo/gost@v1.14.0/time/wheel_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package gxtime 19 20 import ( 21 "sync" 22 "testing" 23 "time" 24 ) 25 26 // output: 27 // timer costs: 30001 ms 28 // --- PASS: TestNewWheel (100.00s) 29 func TestWheel(t *testing.T) { 30 var ( 31 index int 32 wheel *Wheel 33 cw CountWatch 34 ) 35 wheel = NewWheel(TimeMillisecondDuration(100), 20) 36 defer func() { 37 t.Log("timer costs:", cw.Count()/1e6, "ms") 38 wheel.Stop() 39 }() 40 41 cw.Start() 42 for { 43 <-wheel.After(TimeMillisecondDuration(100)) 44 t.Log("loop:", index) 45 index++ 46 if index >= 30 { 47 return 48 } 49 } 50 } 51 52 // output: 53 // timer costs: 45001 ms 54 // --- PASS: TestNewWheel2 (150.00s) 55 func TestWheels(t *testing.T) { 56 var ( 57 wheel *Wheel 58 cw CountWatch 59 wg sync.WaitGroup 60 ) 61 wheel = NewWheel(TimeMillisecondDuration(100), 20) 62 defer func() { 63 t.Log("timer costs:", cw.Count()/1e6, "ms") // 64 wheel.Stop() 65 }() 66 67 f := func(d time.Duration) { 68 defer wg.Done() 69 var index int 70 for { 71 <-wheel.After(d) 72 t.Log("loop:", index, ", interval:", d) 73 index++ 74 if index >= 30 { 75 return 76 } 77 } 78 } 79 80 wg.Add(2) 81 cw.Start() 82 go f(1e9) 83 go f(1510e6) 84 wg.Wait() 85 }