gitlab.com/jokerrs1/Sia@v1.3.2/modules/host/persist_test.go (about) 1 package host 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/NebulousLabs/Sia/modules" 8 ) 9 10 // TestHostContractCountPersistence checks that the host persists its contract 11 // counts correctly 12 func TestHostContractCountPersistence(t *testing.T) { 13 if testing.Short() { 14 t.SkipNow() 15 } 16 ht, err := newHostTester(t.Name()) 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 // add a storage obligation, which should increment contract count 22 so, err := ht.newTesterStorageObligation() 23 if err != nil { 24 t.Fatal(err) 25 } 26 ht.host.managedLockStorageObligation(so.id()) 27 err = ht.host.managedAddStorageObligation(so) 28 if err != nil { 29 t.Fatal(err) 30 } 31 ht.host.managedUnlockStorageObligation(so.id()) 32 33 // should have 1 contract now 34 if ht.host.financialMetrics.ContractCount != 1 { 35 t.Fatal("expected one contract, got", ht.host.financialMetrics.ContractCount) 36 } 37 38 // reload the host 39 err = ht.host.Close() 40 if err != nil { 41 t.Fatal(err) 42 } 43 ht.host, err = New(ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir)) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 // contract count should still be 1 49 if ht.host.financialMetrics.ContractCount != 1 { 50 t.Fatal("expected one contract, got", ht.host.financialMetrics.ContractCount) 51 } 52 } 53 54 // TestHostAddressPersistence checks that the host persists any updates to the 55 // address upon restart. 56 func TestHostAddressPersistence(t *testing.T) { 57 if testing.Short() { 58 t.SkipNow() 59 } 60 ht, err := newHostTester(t.Name()) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 // Set the address of the host. 66 settings := ht.host.InternalSettings() 67 settings.NetAddress = "foo.com:234" 68 err = ht.host.SetInternalSettings(settings) 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 // Reboot the host. 74 err = ht.host.Close() 75 if err != nil { 76 t.Fatal(err) 77 } 78 ht.host, err = New(ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir)) 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 // Verify that the address persisted. 84 if ht.host.settings.NetAddress != "foo.com:234" { 85 t.Error("User-set address does not seem to be persisting.") 86 } 87 }