github.com/NebulousLabs/Sia@v1.3.7/modules/gateway/persist_test.go (about)

     1  package gateway
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/persist"
    10  )
    11  
    12  func TestLoad(t *testing.T) {
    13  	if testing.Short() {
    14  		t.SkipNow()
    15  	}
    16  	t.Parallel()
    17  	g := newTestingGateway(t)
    18  
    19  	g.mu.Lock()
    20  	g.addNode(dummyNode)
    21  	g.saveSync()
    22  	g.mu.Unlock()
    23  	g.Close()
    24  
    25  	g2, err := New("localhost:0", false, g.persistDir)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if _, ok := g2.nodes[dummyNode]; !ok {
    30  		t.Fatal("gateway did not load old peer list:", g2.nodes)
    31  	}
    32  }
    33  
    34  // TestLoadv033 tests that the gateway can load a v033 persist file.
    35  func TestLoadv033(t *testing.T) {
    36  	var buf bytes.Buffer
    37  	log := persist.NewLogger(&buf)
    38  	buf.Reset()
    39  	g := &Gateway{
    40  		nodes:      make(map[modules.NetAddress]*node),
    41  		persistDir: filepath.Join("testdata", t.Name()),
    42  		log:        log,
    43  	}
    44  	if err := g.load(); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	// All nodes should have been loaded
    49  	if len(g.nodes) != 10 {
    50  		t.Error("expected 10 nodes, got", len(g.nodes))
    51  	}
    52  	// All nodes should be marked as non-outbound
    53  	for _, node := range g.nodes {
    54  		if node.WasOutboundPeer {
    55  			t.Error("v033 nodes should not be marked as outbound peers")
    56  		}
    57  	}
    58  
    59  	// The log should be empty
    60  	if buf.Len() != 0 {
    61  		t.Error("expected empty log, got", buf.String())
    62  	}
    63  }