github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/routine_manager_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 frontend
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/BurntSushi/toml"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/config"
    28  	"github.com/matrixorigin/matrixone/pkg/defines"
    29  )
    30  
    31  func create_test_server() *MOServer {
    32  	//before anything using the configuration
    33  	pu := config.NewParameterUnit(&config.FrontendParameters{}, nil, nil, nil)
    34  	_, err := toml.DecodeFile("test/system_vars_config.toml", pu.SV)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	pu.SV.SetDefaultValues()
    39  	setGlobalPu(pu)
    40  
    41  	address := fmt.Sprintf("%s:%d", pu.SV.Host, pu.SV.Port)
    42  	moServerCtx := context.WithValue(context.TODO(), config.ParameterUnitKey, pu)
    43  
    44  	// A mock autoincrcache manager.
    45  	aicm := &defines.AutoIncrCacheManager{}
    46  	return NewMOServer(moServerCtx, address, pu, aicm, nil)
    47  }
    48  
    49  func Test_Closed(t *testing.T) {
    50  	mo := create_test_server()
    51  	getGlobalPu().SV.SkipCheckUser = true
    52  	wg := sync.WaitGroup{}
    53  	wg.Add(1)
    54  	cf := &CloseFlag{}
    55  	go func() {
    56  		cf.Open()
    57  		defer wg.Done()
    58  
    59  		err := mo.Start()
    60  		require.NoError(t, err)
    61  
    62  		for cf.IsOpened() {
    63  		}
    64  	}()
    65  
    66  	time.Sleep(100 * time.Millisecond)
    67  	db, err := openDbConn(t, 6001)
    68  	require.NoError(t, err)
    69  	time.Sleep(100 * time.Millisecond)
    70  	cf.Close()
    71  
    72  	err = mo.Stop()
    73  	require.NoError(t, err)
    74  	wg.Wait()
    75  
    76  	closeDbConn(t, db)
    77  }