github.com/matrixorigin/matrixone@v1.2.0/pkg/queryservice/session_manager_test.go (about)

     1  // Copyright 2021 - 2023 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 queryservice
    16  
    17  import (
    18  	"sync"
    19  	"testing"
    20  
    21  	"github.com/google/uuid"
    22  	"github.com/lni/goutils/leaktest"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/status"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  type mockSession struct {
    28  	id     string
    29  	tenant string
    30  }
    31  
    32  func (s *mockSession) GetUUIDString() string {
    33  	return s.id
    34  }
    35  
    36  func (s *mockSession) GetTenantName() string {
    37  	return s.tenant
    38  }
    39  
    40  func (s *mockSession) StatusSession() *status.Session {
    41  	return &status.Session{
    42  		SessionID: s.id,
    43  		Account:   s.tenant,
    44  	}
    45  }
    46  
    47  func (s *mockSession) SetSessionRoutineStatus(status string) error {
    48  	return nil
    49  }
    50  
    51  func TestNewSessionManager(t *testing.T) {
    52  	sm := NewSessionManager()
    53  	assert.NotNil(t, sm)
    54  	assert.NotNil(t, sm.mu.sessionsByID)
    55  	assert.NotNil(t, sm.mu.sessionsByTenant)
    56  }
    57  
    58  func TestSessionManagerMain(t *testing.T) {
    59  	sm := NewSessionManager()
    60  	assert.NotNil(t, sm)
    61  	s1 := &mockSession{
    62  		id:     uuid.NewString(),
    63  		tenant: "t1",
    64  	}
    65  	sm.AddSession(s1)
    66  	ss := sm.GetAllSessions()
    67  	assert.NotNil(t, ss)
    68  	assert.Equal(t, 1, len(ss))
    69  	assert.Equal(t, s1, ss[0])
    70  
    71  	ss = sm.GetSessionsByTenant("t1")
    72  	assert.NotNil(t, ss)
    73  	assert.Equal(t, 1, len(ss))
    74  	assert.Equal(t, s1, ss[0])
    75  
    76  	ss = sm.GetSessionsByTenant("t2")
    77  	assert.NotNil(t, ss)
    78  	assert.Equal(t, 0, len(ss))
    79  
    80  	sm.RemoveSession(s1)
    81  	ss = sm.GetAllSessions()
    82  	assert.NotNil(t, ss)
    83  	assert.Equal(t, 0, len(ss))
    84  }
    85  
    86  func TestSessionManagerParallel(t *testing.T) {
    87  	defer leaktest.AfterTest(t)()
    88  	sm := NewSessionManager()
    89  	assert.NotNil(t, sm)
    90  	var wg sync.WaitGroup
    91  	count := 100
    92  	for i := 0; i < count; i++ {
    93  		wg.Add(1)
    94  		go func(t *testing.T) {
    95  			sm.AddSession(&mockSession{
    96  				id: uuid.NewString(),
    97  			})
    98  			wg.Done()
    99  		}(t)
   100  	}
   101  	wg.Wait()
   102  	ss := sm.GetAllSessions()
   103  	assert.NotNil(t, ss)
   104  	assert.Equal(t, count, len(ss))
   105  }