github.com/matrixorigin/matrixone@v1.2.0/pkg/common/stopper/stopper_test.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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 stopper
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestRunTaskOnNotRunning(t *testing.T) {
    27  	s := NewStopper("TestRunTaskOnNotRunning")
    28  	s.Stop()
    29  	assert.Equal(t, ErrUnavailable, s.RunTask(func(ctx context.Context) {
    30  
    31  	}))
    32  }
    33  
    34  func TestRunTask(t *testing.T) {
    35  	s := NewStopper("TestRunTask")
    36  	defer s.Stop()
    37  
    38  	c := make(chan struct{})
    39  	assert.NoError(t, s.RunTask(func(ctx context.Context) {
    40  		close(c)
    41  	}))
    42  	select {
    43  	case <-c:
    44  		break
    45  	case <-time.After(time.Second):
    46  		assert.Fail(t, "run task timeout")
    47  	}
    48  }
    49  
    50  func TestRunTaskWithTimeout(t *testing.T) {
    51  	c := make(chan struct{})
    52  	defer close(c)
    53  	var names []string
    54  	s := NewStopper("TestRunTaskWithTimeout",
    55  		WithStopTimeout(time.Millisecond*10),
    56  		WithTimeoutTaskHandler(func(tasks []string, timeAfterStop time.Duration) {
    57  			select {
    58  			case c <- struct{}{}:
    59  			default:
    60  			}
    61  			names = append(names, tasks...)
    62  		}))
    63  
    64  	assert.NoError(t, s.RunNamedTask("timeout", func(ctx context.Context) {
    65  		<-c
    66  	}))
    67  
    68  	s.Stop()
    69  	assert.Equal(t, 1, len(names))
    70  	assert.Equal(t, "timeout", names[0])
    71  }
    72  
    73  func BenchmarkRunTask1000(b *testing.B) {
    74  	for i := 0; i < b.N; i++ {
    75  		runTasks(b, 1000)
    76  	}
    77  }
    78  
    79  func BenchmarkRunTask10000(b *testing.B) {
    80  	for i := 0; i < b.N; i++ {
    81  		runTasks(b, 10000)
    82  	}
    83  }
    84  
    85  func BenchmarkRunTask100000(b *testing.B) {
    86  	for i := 0; i < b.N; i++ {
    87  		runTasks(b, 100000)
    88  	}
    89  }
    90  
    91  func runTasks(b *testing.B, n int) {
    92  	s := NewStopper("BenchmarkRunTask")
    93  	defer s.Stop()
    94  	var wg sync.WaitGroup
    95  	for i := 0; i < n; i++ {
    96  		wg.Add(1)
    97  		assert.NoError(b, s.RunTask(func(ctx context.Context) {
    98  			wg.Done()
    99  			<-ctx.Done()
   100  		}))
   101  	}
   102  	wg.Wait()
   103  }