Skip to content
Snippets Groups Projects
Select Git revision
  • c565c1ea5568bdbfd9740f7b9b6be8429ea49a35
  • release default
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • master protected
  • XX-4688/DbEncoding
  • hotfix/update
  • @XX-4682/Files
  • hotfix/XX-4655
  • dev protected
  • project/HavenNotifications
  • XX-4602/SilentMessageType
  • jono/npmTest
  • wasmTest2
  • XX-4461/FileUpload
  • XX-4505/blockuser
  • XX-4441
  • Jakub/Emoji-CI-Test
  • testing/websockets
  • fastReg
  • fast-registration
  • NewHostPool
  • v0.3.22
  • v0.3.21
  • v0.3.20
  • v0.3.18
  • v0.3.17
  • v0.3.16
  • v0.3.15
  • v0.3.14
  • v0.3.13
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • 812b395df518ce096d01d5292596ca26f8fe92d9c4487ddfa515e190a51aa1a1
  • 76ba08e2dfa1798412a265404fa271840b52c035869111fce8e8cdb23a036a5a
41 results

implementation.go

Blame
  • connectionList_test.go 4.05 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 xx foundation                                             //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file.                                                              //
    ////////////////////////////////////////////////////////////////////////////////
    
    package connect
    
    import (
    	"gitlab.com/xx_network/primitives/id"
    	"gitlab.com/xx_network/primitives/netTime"
    	"reflect"
    	"testing"
    	"time"
    )
    
    // Tests that NewConnectionList returned the expected new ConnectionList.
    func TestNewConnectionList(t *testing.T) {
    	expected := &ConnectionList{
    		list: make(map[id.ID]Connection),
    		p:    DefaultConnectionListParams(),
    	}
    
    	cl := NewConnectionList(expected.p)
    
    	if !reflect.DeepEqual(expected, cl) {
    		t.Errorf("New ConnectionList did not match expected."+
    			"\nexpected: %+v\nreceived: %+v", expected, cl)
    	}
    }
    
    // Tests that ConnectionList.Add adds all the given connections to the list.
    func TestConnectionList_Add(t *testing.T) {
    	cl := NewConnectionList(DefaultConnectionListParams())
    
    	expected := map[id.ID]Connection{
    		*id.NewIdFromString("p1", id.User, t): &handler{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p1", id.User, t)}},
    		*id.NewIdFromString("p2", id.User, t): &handler{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p2", id.User, t)}},
    		*id.NewIdFromString("p3", id.User, t): &handler{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p3", id.User, t)}},
    		*id.NewIdFromString("p4", id.User, t): &handler{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p4", id.User, t)}},
    		*id.NewIdFromString("p5", id.User, t): &handler{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p5", id.User, t)}},
    	}
    
    	for _, c := range expected {
    		cl.Add(c)
    	}
    
    	if !reflect.DeepEqual(expected, cl.list) {
    		t.Errorf("List does not have expected connections."+
    			"\nexpected: %+v\nreceived: %+v", expected, cl.list)
    	}
    
    }
    
    // Tests that ConnectionList.Cleanup deletes only stale connections from the
    // list and that they are closed.
    func TestConnectionList_Cleanup(t *testing.T) {
    	cl := NewConnectionList(DefaultConnectionListParams())
    
    	list := []*mockConnection{
    		{
    			partner: &mockPartner{partnerId: id.NewIdFromString("p0", id.User, t)},
    			lastUse: netTime.Now().Add(-(cl.p.MaxAge * 2)),
    		}, {
    			partner: &mockPartner{partnerId: id.NewIdFromString("p1", id.User, t)},
    			lastUse: netTime.Now().Add(-(cl.p.MaxAge / 2)),
    		}, {
    			partner: &mockPartner{partnerId: id.NewIdFromString("p2", id.User, t)},
    			lastUse: netTime.Now().Add(-(cl.p.MaxAge + 10)),
    		}, {
    			partner: &mockPartner{partnerId: id.NewIdFromString("p3", id.User, t)},
    			lastUse: netTime.Now().Add(-(cl.p.MaxAge - time.Second)),
    		},
    	}
    
    	for _, c := range list {
    		cl.Add(c)
    	}
    
    	cl.Cleanup()
    
    	for i, c := range list {
    		if i%2 == 0 {
    			if _, exists := cl.list[*c.GetPartner().PartnerId()]; exists {
    				t.Errorf("Connection #%d exists while being stale.", i)
    			}
    			if !c.closed {
    				t.Errorf("Connection #%d was not closed.", i)
    			}
    		} else {
    			if _, exists := cl.list[*c.GetPartner().PartnerId()]; !exists {
    				t.Errorf("Connection #%d was removed when it was not stale.", i)
    			}
    			if c.closed {
    				t.Errorf("Connection #%d was closed.", i)
    			}
    		}
    	}
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    // Parameters                                                                 //
    ////////////////////////////////////////////////////////////////////////////////
    
    // Tests that DefaultConnectionListParams returns a ConnectionListParams with
    // the expected default values.
    func TestDefaultConnectionListParams(t *testing.T) {
    	expected := ConnectionListParams{
    		CleanupPeriod: cleanupPeriodDefault,
    		MaxAge:        maxAgeDefault,
    	}
    
    	p := DefaultConnectionListParams()
    
    	if !reflect.DeepEqual(expected, p) {
    		t.Errorf("Default ConnectionListParams does not match expected."+
    			"\nexpected: %+v\nreceived: %+v", expected, p)
    	}
    }