github.com/dubbogo/gost@v1.14.0/container/gxsync/batcher_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  	"testing"
    38  	"time"
    39  )
    40  
    41  import (
    42  	"go.uber.org/atomic"
    43  )
    44  
    45  // makeAfterFnWithLatch returns a fake alternative to time.After that blocks until
    46  // the release function is called. The fake doesn't support having multiple concurrent
    47  // calls to the After function, which is ok because Batcher should never do that.
    48  func makeAfterFnWithLatch(t *testing.T) (func(time.Duration) <-chan time.Time, func()) {
    49  	latch := make(chan time.Time, 1)
    50  	afterFn := func(d time.Duration) <-chan time.Time {
    51  		return latch
    52  	}
    53  
    54  	releaseFn := func() {
    55  		select {
    56  		case latch <- time.Now():
    57  		default:
    58  			t.Errorf("Previous batch still hasn't been released")
    59  		}
    60  	}
    61  	return afterFn, releaseFn
    62  }
    63  
    64  func TestBatcher(t *testing.T) {
    65  	interval := time.Duration(50 * time.Millisecond)
    66  
    67  	afterFn, releaseBatch := makeAfterFnWithLatch(t)
    68  	b := newBatcherForTest(interval, afterFn)
    69  
    70  	waitersFinished := atomic.NewInt32(0)
    71  
    72  	startWaiter := func(testcase string, want int) {
    73  		go func() {
    74  			id := b.Wait()
    75  			if id != want {
    76  				t.Errorf("%s: got %d, want %d", testcase, id, want)
    77  			}
    78  			waitersFinished.Add(1)
    79  		}()
    80  	}
    81  
    82  	awaitVal := func(name string, val *atomic.Int32, expected int32) {
    83  		for count := 0; val.Load() != expected; count++ {
    84  			time.Sleep(50 * time.Millisecond)
    85  			if count > 5 {
    86  				t.Errorf("Timed out waiting for %s to be %v", name, expected)
    87  				return
    88  			}
    89  		}
    90  	}
    91  
    92  	awaitBatch := func(name string, n int32) {
    93  		// Wait for all the waiters to register
    94  		awaitVal("Batcher.waiters for "+name, b.waiters, n)
    95  		// Release the batch and wait for the batcher to catch up.
    96  		if waitersFinished.Load() != 0 {
    97  			t.Errorf("Waiters finished before being released")
    98  		}
    99  		releaseBatch()
   100  		awaitVal("Batcher.waiters for "+name, b.waiters, 0)
   101  		// Make sure the waiters actually run so they can verify their batch number.
   102  		awaitVal("waitersFinshed for "+name, waitersFinished, n)
   103  		waitersFinished.Swap(0)
   104  	}
   105  
   106  	// test single waiter
   107  	startWaiter("single waiter", 1)
   108  	awaitBatch("single waiter", 1)
   109  
   110  	// multiple waiters all at once
   111  	startWaiter("concurrent waiter", 2)
   112  	startWaiter("concurrent waiter", 2)
   113  	startWaiter("concurrent waiter", 2)
   114  	awaitBatch("concurrent waiter", 3)
   115  
   116  	startWaiter("more waiters", 3)
   117  	startWaiter("more waiters", 3)
   118  	startWaiter("more waiters", 3)
   119  	startWaiter("more waiters", 3)
   120  	startWaiter("more waiters", 3)
   121  	awaitBatch("more waiters", 5)
   122  }