github.com/matrixorigin/matrixone@v1.2.0/pkg/taskservice/task_service_holder_test.go (about)

     1  // Copyright 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 taskservice
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/lni/goutils/leaktest"
    22  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    23  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestTaskHolderCanCreateTaskService(t *testing.T) {
    29  	defer leaktest.AfterTest(t)()
    30  	store := NewMemTaskStorage()
    31  	h := NewTaskServiceHolderWithTaskStorageFactorySelector(
    32  		runtime.DefaultRuntime(),
    33  		func(ctx context.Context, random bool) (string, error) { return "", nil },
    34  		func(s1, s2, s3 string) TaskStorageFactory {
    35  			return NewFixedTaskStorageFactory(store)
    36  		})
    37  	require.NoError(t, h.Create(logservicepb.CreateTaskService{
    38  		User:         logservicepb.TaskTableUser{Username: "u", Password: "p"},
    39  		TaskDatabase: "d",
    40  	}))
    41  	defer func() {
    42  		require.NoError(t, h.Close())
    43  	}()
    44  	s, ok := h.Get()
    45  	assert.True(t, ok)
    46  	assert.NotNil(t, s)
    47  	assert.Equal(t, store, s.GetStorage().(*refreshableTaskStorage).mu.store)
    48  }
    49  
    50  func TestTaskHolderCreateWithEmptyCommandReturnError(t *testing.T) {
    51  	store := NewMemTaskStorage()
    52  	h := NewTaskServiceHolderWithTaskStorageFactorySelector(
    53  		runtime.DefaultRuntime(),
    54  		func(context.Context, bool) (string, error) { return "", nil },
    55  		func(s1, s2, s3 string) TaskStorageFactory {
    56  			return NewFixedTaskStorageFactory(store)
    57  		})
    58  	assert.Error(t, h.Create(logservicepb.CreateTaskService{}))
    59  }
    60  
    61  func TestTaskHolderNotCreatedCanClose(t *testing.T) {
    62  	store := NewMemTaskStorage()
    63  	h := NewTaskServiceHolderWithTaskStorageFactorySelector(
    64  		runtime.DefaultRuntime(),
    65  		func(context.Context, bool) (string, error) { return "", nil },
    66  		func(s1, s2, s3 string) TaskStorageFactory {
    67  			return NewFixedTaskStorageFactory(store)
    68  		})
    69  	assert.NoError(t, h.Close())
    70  }
    71  
    72  func TestTaskHolderCanClose(t *testing.T) {
    73  	store := NewMemTaskStorage()
    74  	h := NewTaskServiceHolderWithTaskStorageFactorySelector(
    75  		runtime.DefaultRuntime(),
    76  		func(context.Context, bool) (string, error) { return "", nil },
    77  		func(s1, s2, s3 string) TaskStorageFactory {
    78  			return NewFixedTaskStorageFactory(store)
    79  		})
    80  	require.NoError(t, h.Create(logservicepb.CreateTaskService{
    81  		User:         logservicepb.TaskTableUser{Username: "u", Password: "p"},
    82  		TaskDatabase: "d",
    83  	}))
    84  	assert.NoError(t, h.Close())
    85  }
    86  
    87  func TestRefreshTaskStorageCanRefresh(t *testing.T) {
    88  	defer leaktest.AfterTest(t)()
    89  	ctx := context.TODO()
    90  
    91  	stores := map[string]TaskStorage{
    92  		"s1": NewMemTaskStorage(),
    93  		"s2": NewMemTaskStorage(),
    94  	}
    95  	address := "s1"
    96  	s := newRefreshableTaskStorage(
    97  		runtime.DefaultRuntime(),
    98  		func(context.Context, bool) (string, error) { return address, nil },
    99  		&testStorageFactory{stores: stores}).(*refreshableTaskStorage)
   100  	defer func() {
   101  		require.NoError(t, s.Close())
   102  	}()
   103  
   104  	s.mu.RLock()
   105  	assert.Equal(t, stores["s1"], s.mu.store)
   106  	assert.Equal(t, "s1", s.mu.lastAddress)
   107  	s.mu.RUnlock()
   108  
   109  	s.refresh(ctx, "s2")
   110  	s.mu.RLock()
   111  	assert.Equal(t, stores["s1"], s.mu.store)
   112  	assert.Equal(t, "s1", s.mu.lastAddress)
   113  	s.mu.RUnlock()
   114  
   115  	address = "s2"
   116  	s.refresh(ctx, "s1")
   117  	s.mu.RLock()
   118  	assert.Equal(t, stores["s2"], s.mu.store)
   119  	assert.Equal(t, "s2", s.mu.lastAddress)
   120  	s.mu.RUnlock()
   121  }
   122  
   123  func TestRefreshTaskStorageCanClose(t *testing.T) {
   124  	stores := map[string]TaskStorage{
   125  		"s1": NewMemTaskStorage(),
   126  		"s2": NewMemTaskStorage(),
   127  	}
   128  	address := "s1"
   129  	s := newRefreshableTaskStorage(
   130  		runtime.DefaultRuntime(),
   131  		func(context.Context, bool) (string, error) { return address, nil },
   132  		&testStorageFactory{stores: stores}).(*refreshableTaskStorage)
   133  	address = "s2"
   134  	require.True(t, s.maybeRefresh("s1"))
   135  	require.NoError(t, s.Close())
   136  	<-s.refreshC
   137  }
   138  
   139  type testStorageFactory struct {
   140  	stores map[string]TaskStorage
   141  }
   142  
   143  func (f *testStorageFactory) Create(address string) (TaskStorage, error) {
   144  	return f.stores[address], nil
   145  }