github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/connectivity/status_storage_test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package connectivity
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/mysteriumnetwork/node/identity"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestStatusStorage_AddStatusEntry(t *testing.T) {
    29  	storage := NewStatusStorage()
    30  	e1 := StatusEntry{
    31  		PeerID:       identity.Identity{},
    32  		SessionID:    "1",
    33  		StatusCode:   StatusConnectionOk,
    34  		Message:      "Ok",
    35  		CreatedAtUTC: time.Now().UTC(),
    36  	}
    37  	e2 := StatusEntry{
    38  		PeerID:       identity.Identity{},
    39  		SessionID:    "",
    40  		StatusCode:   StatusConnectionFailed,
    41  		Message:      "Failed",
    42  		CreatedAtUTC: time.Now().UTC().Add(-1 * time.Second),
    43  	}
    44  
    45  	storage.AddStatusEntry(e1)
    46  	storage.AddStatusEntry(e2)
    47  
    48  	entries := storage.GetAllStatusEntries()
    49  	assert.Len(t, entries, 2)
    50  	assert.Equal(t, e1, entries[0])
    51  	assert.Equal(t, e2, entries[1])
    52  }
    53  
    54  func TestStatusStorage_AddStatusEntry_RemovesOldEntries(t *testing.T) {
    55  	storage := NewStatusStorage()
    56  	e1 := StatusEntry{
    57  		SessionID:    "s1",
    58  		CreatedAtUTC: time.Now().UTC().Add(-maxEntriesKeepDuration * 2),
    59  	}
    60  	storage.AddStatusEntry(e1)
    61  
    62  	e2 := StatusEntry{
    63  		SessionID:    "s2",
    64  		CreatedAtUTC: time.Now().UTC(),
    65  	}
    66  	storage.AddStatusEntry(e2)
    67  
    68  	entries := storage.GetAllStatusEntries()
    69  	assert.Len(t, entries, 1)
    70  	assert.Equal(t, e2, entries[0])
    71  }
    72  
    73  func TestStatusStorage_GetAllStatusEntries_Returns_Immutable_Data(t *testing.T) {
    74  	storage := NewStatusStorage()
    75  	e1 := StatusEntry{
    76  		PeerID:       identity.Identity{},
    77  		SessionID:    "1",
    78  		StatusCode:   StatusConnectionOk,
    79  		Message:      "Ok",
    80  		CreatedAtUTC: time.Time{},
    81  	}
    82  	storage.AddStatusEntry(e1)
    83  
    84  	entries := storage.GetAllStatusEntries()
    85  
    86  	entries[0].SessionID = "2"
    87  	assert.NotEqual(t, entries[0].SessionID, storage.(*statusStorage).entries[0].SessionID)
    88  }
    89  
    90  func TestStatusStorage_GetAllStatusEntries_Returns_Sorted_Data(t *testing.T) {
    91  	storage := NewStatusStorage()
    92  	e1 := StatusEntry{
    93  		SessionID:    "1",
    94  		CreatedAtUTC: time.Now(),
    95  	}
    96  	e2 := StatusEntry{
    97  		SessionID:    "2",
    98  		CreatedAtUTC: time.Now().Add(-10 * time.Minute),
    99  	}
   100  	e3 := StatusEntry{
   101  		SessionID:    "3",
   102  		CreatedAtUTC: time.Now().Add(15 * time.Minute),
   103  	}
   104  	storage.AddStatusEntry(e1)
   105  	storage.AddStatusEntry(e2)
   106  	storage.AddStatusEntry(e3)
   107  
   108  	entries := storage.GetAllStatusEntries()
   109  
   110  	assert.Equal(t, e3, entries[0])
   111  	assert.Equal(t, e1, entries[1])
   112  	assert.Equal(t, e2, entries[2])
   113  }