github.com/dolthub/go-mysql-server@v0.18.0/sql/background_threads_test.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     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 sql_test
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"sort"
    21  	"sync"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"github.com/dolthub/go-mysql-server/sql"
    27  )
    28  
    29  func TestBackgroundThreads(t *testing.T) {
    30  	var bThreads *sql.BackgroundThreads
    31  	var err error
    32  
    33  	var b []int
    34  	mu := &sync.Mutex{}
    35  	f := func(i int) func(ctx context.Context) {
    36  		return func(ctx context.Context) {
    37  			for {
    38  				select {
    39  				case <-ctx.Done():
    40  					mu.Lock()
    41  					defer mu.Unlock()
    42  					b = append(b, i)
    43  					return
    44  				}
    45  			}
    46  		}
    47  	}
    48  
    49  	t.Run("add, close", func(t *testing.T) {
    50  		b = make([]int, 0)
    51  		bThreads = sql.NewBackgroundThreads()
    52  		defer bThreads.Shutdown()
    53  
    54  		err = bThreads.Add("first", f(1))
    55  		assert.NoError(t, err)
    56  
    57  		err = bThreads.Add("second", f(2))
    58  		assert.NoError(t, err)
    59  
    60  		// wait until close to flush
    61  		assert.Equal(t, []int{}, b)
    62  
    63  		err = bThreads.Shutdown()
    64  		assert.True(t, errors.Is(err, context.Canceled))
    65  
    66  		sort.Ints(b)
    67  		assert.Equal(t, []int{1, 2}, b)
    68  	})
    69  
    70  	t.Run("close is idempotent", func(t *testing.T) {
    71  		b = make([]int, 0)
    72  		bThreads = sql.NewBackgroundThreads()
    73  		defer bThreads.Shutdown()
    74  
    75  		err = bThreads.Add("first", f(1))
    76  		assert.NoError(t, err)
    77  
    78  		err = bThreads.Shutdown()
    79  		assert.True(t, errors.Is(err, context.Canceled))
    80  		err = bThreads.Shutdown()
    81  		assert.True(t, errors.Is(err, context.Canceled))
    82  
    83  		sort.Ints(b)
    84  		assert.Equal(t, []int{1}, b)
    85  	})
    86  
    87  	t.Run("can't add after closed", func(t *testing.T) {
    88  		b = make([]int, 0)
    89  		bThreads = sql.NewBackgroundThreads()
    90  		defer bThreads.Shutdown()
    91  
    92  		err = bThreads.Shutdown()
    93  		assert.True(t, errors.Is(err, context.Canceled))
    94  
    95  		err = bThreads.Add("first", f(1))
    96  		assert.True(t, errors.Is(err, sql.ErrCannotAddToClosedBackgroundThreads))
    97  
    98  		sort.Ints(b)
    99  		assert.Equal(t, []int{}, b)
   100  	})
   101  }