github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/hostdb/hostentry_test.go (about)

     1  package hostdb
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/Sia/types"
     9  )
    10  
    11  // TestInsertHost tests the insertHost method, which also depends on the
    12  // scanHostEntry method.
    13  func TestInsertHost(t *testing.T) {
    14  	hdb := bareHostDB()
    15  
    16  	// invalid host should not be scanned
    17  	var dbe modules.HostDBEntry
    18  	dbe.NetAddress = "foo"
    19  	hdb.insertHost(dbe)
    20  	select {
    21  	case <-hdb.scanPool:
    22  		t.Error("invalid host was added to scan pool")
    23  	case <-time.After(100 * time.Millisecond):
    24  	}
    25  
    26  	// valid host should be scanned
    27  	dbe.NetAddress = "foo.com:1234"
    28  	hdb.insertHost(dbe)
    29  	select {
    30  	case <-hdb.scanPool:
    31  	case <-time.After(time.Second):
    32  		t.Error("host was not scanned")
    33  	}
    34  
    35  	// duplicate host should not be scanned
    36  	hdb.insertHost(dbe)
    37  	select {
    38  	case <-hdb.scanPool:
    39  		t.Error("duplicate host was added to scan pool")
    40  	case <-time.After(100 * time.Millisecond):
    41  	}
    42  }
    43  
    44  // TestActiveHosts tests the ActiveHosts method.
    45  func TestActiveHosts(t *testing.T) {
    46  	hdb := bareHostDB()
    47  
    48  	// empty
    49  	if hosts := hdb.ActiveHosts(); len(hosts) != 0 {
    50  		t.Errorf("wrong number of hosts: expected %v, got %v", 0, len(hosts))
    51  	}
    52  
    53  	// with one host
    54  	h1 := new(hostEntry)
    55  	h1.NetAddress = "foo"
    56  	hdb.activeHosts = map[modules.NetAddress]*hostNode{
    57  		h1.NetAddress: {hostEntry: h1},
    58  	}
    59  	if hosts := hdb.ActiveHosts(); len(hosts) != 1 {
    60  		t.Errorf("wrong number of hosts: expected %v, got %v", 1, len(hosts))
    61  	} else if hosts[0].NetAddress != h1.NetAddress {
    62  		t.Errorf("ActiveHosts returned wrong host: expected %v, got %v", h1.NetAddress, hosts[0].NetAddress)
    63  	}
    64  
    65  	// with multiple hosts
    66  	h2 := new(hostEntry)
    67  	h2.NetAddress = "bar"
    68  	hdb.activeHosts = map[modules.NetAddress]*hostNode{
    69  		h1.NetAddress: {hostEntry: h1},
    70  		h2.NetAddress: {hostEntry: h2},
    71  	}
    72  	if hosts := hdb.ActiveHosts(); len(hosts) != 2 {
    73  		t.Errorf("wrong number of hosts: expected %v, got %v", 2, len(hosts))
    74  	} else if hosts[0].NetAddress != h1.NetAddress && hosts[1].NetAddress != h1.NetAddress {
    75  		t.Errorf("ActiveHosts did not contain an inserted host: %v (missing %v)", hosts, h1.NetAddress)
    76  	} else if hosts[0].NetAddress != h2.NetAddress && hosts[1].NetAddress != h2.NetAddress {
    77  		t.Errorf("ActiveHosts did not contain an inserted host: %v (missing %v)", hosts, h2.NetAddress)
    78  	}
    79  }
    80  
    81  // TestAveragePrice tests the AveragePrice method, which also depends on the
    82  // randomHosts method.
    83  func TestAveragePrice(t *testing.T) {
    84  	hdb := bareHostDB()
    85  
    86  	// empty
    87  	if avg := hdb.AveragePrice(); !avg.IsZero() {
    88  		t.Error("average of empty hostdb should be zero:", avg)
    89  	}
    90  
    91  	// with one host
    92  	h1 := new(hostEntry)
    93  	h1.NetAddress = "foo"
    94  	h1.ContractPrice = types.NewCurrency64(100)
    95  	h1.Weight = baseWeight
    96  	hdb.insertNode(h1)
    97  	if avg := hdb.AveragePrice(); avg.Cmp(h1.ContractPrice) != 0 {
    98  		t.Error("average of one host should be that host's price:", avg)
    99  	}
   100  
   101  	// with two hosts
   102  	h2 := new(hostEntry)
   103  	h2.NetAddress = "bar"
   104  	h2.ContractPrice = types.NewCurrency64(300)
   105  	h2.Weight = baseWeight
   106  	hdb.insertNode(h2)
   107  	if len(hdb.activeHosts) != 2 {
   108  		t.Error("host was not added:", hdb.activeHosts)
   109  	}
   110  	if avg := hdb.AveragePrice(); avg.Cmp(types.NewCurrency64(200)) != 0 {
   111  		t.Error("average of two hosts should be their sum/2:", avg)
   112  	}
   113  }
   114  
   115  // TestIsOffline tests the IsOffline method.
   116  func TestIsOffline(t *testing.T) {
   117  	hdb := &HostDB{
   118  		allHosts: map[modules.NetAddress]*hostEntry{
   119  			"foo.com:1234": {Online: true},
   120  			"bar.com:1234": {Online: false},
   121  			"baz.com:1234": {Online: true},
   122  		},
   123  		activeHosts: map[modules.NetAddress]*hostNode{
   124  			"foo.com:1234": nil,
   125  		},
   126  		scanPool: make(chan *hostEntry),
   127  	}
   128  
   129  	tests := []struct {
   130  		addr    modules.NetAddress
   131  		offline bool
   132  	}{
   133  		{"foo.com:1234", false},
   134  		{"bar.com:1234", true},
   135  		{"baz.com:1234", false},
   136  		{"quux.com:1234", false},
   137  	}
   138  	for _, test := range tests {
   139  		if offline := hdb.IsOffline(test.addr); offline != test.offline {
   140  			t.Errorf("IsOffline(%v) = %v, expected %v", test.addr, offline, test.offline)
   141  		}
   142  	}
   143  }