gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/gateway/persist_test.go (about)

     1  package gateway
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"gitlab.com/SiaPrime/SiaPrime/modules"
    10  	"gitlab.com/SiaPrime/SiaPrime/persist"
    11  )
    12  
    13  func TestLoad(t *testing.T) {
    14  	if testing.Short() {
    15  		t.SkipNow()
    16  	}
    17  	t.Parallel()
    18  
    19  	// Start new gateway
    20  	g := newTestingGateway(t)
    21  
    22  	// Add node and persist node and gateway
    23  	g.mu.Lock()
    24  	g.addNode(dummyNode)
    25  	if err := g.saveSync(); err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if err := g.saveSyncNodes(); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	g.mu.Unlock()
    32  	g.Close()
    33  
    34  	// Start second gateway
    35  	g2, err := New("localhost:0", false, g.persistDir)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	// Confirm node from gateway 1 is in gateway 2
    41  	if _, ok := g2.nodes[dummyNode]; !ok {
    42  		t.Fatal("gateway did not load old peer list:", g2.nodes)
    43  	}
    44  
    45  	// Confirm the persisted gateway information is the same between the two
    46  	// gateways
    47  	if !reflect.DeepEqual(g.persist, g2.persist) {
    48  		t.Log("g.persit:", g.persist)
    49  		t.Log("g2.persit:", g2.persist)
    50  		t.Fatal("Gateway not persisted")
    51  	}
    52  }
    53  
    54  // TestLoadv033 tests that the gateway can load a v033 persist file for the node
    55  // persistence.
    56  func TestLoadv033(t *testing.T) {
    57  	var buf bytes.Buffer
    58  	log := persist.NewLogger(&buf)
    59  	buf.Reset()
    60  	g := &Gateway{
    61  		nodes:      make(map[modules.NetAddress]*node),
    62  		persistDir: filepath.Join("testdata", t.Name()),
    63  		log:        log,
    64  	}
    65  	if err := g.load(); err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	// All nodes should have been loaded
    70  	if len(g.nodes) != 10 {
    71  		t.Error("expected 10 nodes, got", len(g.nodes))
    72  	}
    73  	// All nodes should be marked as non-outbound
    74  	for _, node := range g.nodes {
    75  		if node.WasOutboundPeer {
    76  			t.Error("v033 nodes should not be marked as outbound peers")
    77  		}
    78  	}
    79  
    80  	// The log should be empty
    81  	if buf.Len() != 0 {
    82  		t.Error("expected empty log, got", buf.String())
    83  	}
    84  }