github.com/braveheart12/insolar-09-08-19@v0.8.7/network/controller/bootstrap/session_manager_test.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package bootstrap
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/insolar/insolar/core"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func sessionMapLen(sm SessionManager) int {
    31  	s := sm.(*sessionManager)
    32  
    33  	s.lock.RLock()
    34  	defer s.lock.RUnlock()
    35  
    36  	return len(s.sessions)
    37  }
    38  
    39  func sessionMapDelete(sm SessionManager, id SessionID) {
    40  	s := sm.(*sessionManager)
    41  
    42  	s.lock.Lock()
    43  	defer s.lock.Unlock()
    44  
    45  	delete(s.sessions, id)
    46  }
    47  
    48  func TestNewSessionManager(t *testing.T) {
    49  	sm := NewSessionManager().(*sessionManager)
    50  
    51  	assert.Equal(t, sm.state, stateIdle)
    52  }
    53  
    54  func TestSessionManager_CleanupSimple(t *testing.T) {
    55  	sm := NewSessionManager()
    56  
    57  	err := sm.Start(context.Background())
    58  	require.NoError(t, err)
    59  
    60  	sm.NewSession(core.RecordRef{}, nil, time.Second)
    61  	require.Equal(t, sessionMapLen(sm), 1)
    62  
    63  	time.Sleep(1500 * time.Millisecond)
    64  	assert.Equal(t, sessionMapLen(sm), 0)
    65  
    66  	err = sm.Stop(context.Background())
    67  	require.NoError(t, err)
    68  }
    69  
    70  func TestSessionManager_CleanupConcurrent(t *testing.T) {
    71  	sm := NewSessionManager()
    72  
    73  	err := sm.Start(context.Background())
    74  	require.NoError(t, err)
    75  
    76  	id := sm.NewSession(core.RecordRef{}, nil, time.Second)
    77  	require.Equal(t, sessionMapLen(sm), 1)
    78  
    79  	// delete session here and check nothing happened
    80  	sessionMapDelete(sm, id)
    81  
    82  	time.Sleep(1500 * time.Millisecond)
    83  	assert.Equal(t, sessionMapLen(sm), 0)
    84  
    85  	err = sm.Stop(context.Background())
    86  	require.NoError(t, err)
    87  }
    88  
    89  func TestSessionManager_CleanupOrder(t *testing.T) {
    90  	sm := NewSessionManager()
    91  
    92  	err := sm.Start(context.Background())
    93  	require.NoError(t, err)
    94  
    95  	sm.NewSession(core.RecordRef{}, nil, 2*time.Second)
    96  	sm.NewSession(core.RecordRef{}, nil, 2*time.Second)
    97  	sm.NewSession(core.RecordRef{}, nil, time.Second)
    98  	require.Equal(t, sessionMapLen(sm), 3)
    99  
   100  	time.Sleep(1500 * time.Millisecond)
   101  	assert.Equal(t, sessionMapLen(sm), 2)
   102  
   103  	err = sm.Stop(context.Background())
   104  	require.NoError(t, err)
   105  }
   106  
   107  func TestSessionManager_ImmediatelyStop(t *testing.T) {
   108  	sm := NewSessionManager()
   109  
   110  	err := sm.Start(context.Background())
   111  	require.NoError(t, err)
   112  
   113  	err = sm.Stop(context.Background())
   114  	require.NoError(t, err)
   115  }
   116  
   117  func TestSessionManager_DoubleStart(t *testing.T) {
   118  	sm := NewSessionManager()
   119  
   120  	err := sm.Start(context.Background())
   121  	require.NoError(t, err)
   122  
   123  	err = sm.Start(context.Background())
   124  	require.NoError(t, err)
   125  }
   126  
   127  func TestSessionManager_DoubleStop(t *testing.T) {
   128  	sm := NewSessionManager()
   129  
   130  	err := sm.Start(context.Background())
   131  	require.NoError(t, err)
   132  
   133  	err = sm.Stop(context.Background())
   134  	require.NoError(t, err)
   135  
   136  	err = sm.Stop(context.Background())
   137  	require.NoError(t, err)
   138  }