vitess.io/vitess@v0.16.2/go/sync2/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  package sync2
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  // makeAfterFnWithLatch returns a fake alternative to time.After that blocks until
    25  // the release function is called. The fake doesn't support having multiple concurrent
    26  // calls to the After function, which is ok because Batcher should never do that.
    27  func makeAfterFnWithLatch(t *testing.T) (func(time.Duration) <-chan time.Time, func()) {
    28  	latch := make(chan time.Time, 1)
    29  	afterFn := func(d time.Duration) <-chan time.Time {
    30  		return latch
    31  	}
    32  
    33  	releaseFn := func() {
    34  		select {
    35  		case latch <- time.Now():
    36  		default:
    37  			t.Errorf("Previous batch still hasn't been released")
    38  		}
    39  	}
    40  	return afterFn, releaseFn
    41  }
    42  
    43  func TestBatcher(t *testing.T) {
    44  	interval := time.Duration(50 * time.Millisecond)
    45  
    46  	afterFn, releaseBatch := makeAfterFnWithLatch(t)
    47  	b := newBatcherForTest(interval, afterFn)
    48  
    49  	waitersFinished := NewAtomicInt32(0)
    50  
    51  	startWaiter := func(testcase string, want int) {
    52  		go func() {
    53  			id := b.Wait()
    54  			if id != want {
    55  				t.Errorf("%s: got %d, want %d", testcase, id, want)
    56  			}
    57  			waitersFinished.Add(1)
    58  		}()
    59  	}
    60  
    61  	awaitVal := func(name string, val *AtomicInt32, expected int32) {
    62  		for count := 0; val.Get() != expected; count++ {
    63  			time.Sleep(50 * time.Millisecond)
    64  			if count > 5 {
    65  				t.Errorf("Timed out waiting for %s to be %v", name, expected)
    66  				return
    67  			}
    68  		}
    69  	}
    70  
    71  	awaitBatch := func(name string, n int32) {
    72  		// Wait for all the waiters to register
    73  		awaitVal("Batcher.waiters for "+name, &b.waiters, n)
    74  		// Release the batch and wait for the batcher to catch up.
    75  		if waitersFinished.Get() != 0 {
    76  			t.Errorf("Waiters finished before being released")
    77  		}
    78  		releaseBatch()
    79  		awaitVal("Batcher.waiters for "+name, &b.waiters, 0)
    80  		// Make sure the waiters actually run so they can verify their batch number.
    81  		awaitVal("waitersFinshed for "+name, &waitersFinished, n)
    82  		waitersFinished.Set(0)
    83  	}
    84  
    85  	// test single waiter
    86  	startWaiter("single waiter", 1)
    87  	awaitBatch("single waiter", 1)
    88  
    89  	// multiple waiters all at once
    90  	startWaiter("concurrent waiter", 2)
    91  	startWaiter("concurrent waiter", 2)
    92  	startWaiter("concurrent waiter", 2)
    93  	awaitBatch("concurrent waiter", 3)
    94  
    95  	startWaiter("more waiters", 3)
    96  	startWaiter("more waiters", 3)
    97  	startWaiter("more waiters", 3)
    98  	startWaiter("more waiters", 3)
    99  	startWaiter("more waiters", 3)
   100  	awaitBatch("more waiters", 5)
   101  }