github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/cmd/bs/bs_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	holo "github.com/holochain/holochain-proto"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	_ "github.com/urfave/cli"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestSetupApp(t *testing.T) {
    14  	app := setupApp()
    15  	Convey("it should create the bootstrap server App", t, func() {
    16  		So(app.Name, ShouldEqual, "bs")
    17  	})
    18  }
    19  
    20  func TestPostGet(t *testing.T) {
    21  	d := holo.SetupTestDir()
    22  	defer holo.CleanupTestDir(d)
    23  
    24  	Convey("it should setup the db", t, func() {
    25  		err := setupDB(d + "bsdb.buntdb")
    26  		So(err, ShouldBeNil)
    27  	})
    28  
    29  	halfOfBoostrapTTL := holo.BootstrapTTL - holo.BootstrapTTL/2
    30  
    31  	Convey("it should store and retrieve and ignore old stuff", t, func() {
    32  		chain := "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXax"
    33  		req1 := holo.BSReq{Version: 1, NodeID: "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx", NodeAddr: "192.168.1.1"}
    34  		now := time.Now()
    35  		then := now.Add(-halfOfBoostrapTTL)
    36  
    37  		err := post(chain, &req1, "172.3.4.1", then)
    38  		So(err, ShouldBeNil)
    39  
    40  		req2 := holo.BSReq{Version: 1, NodeID: "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHy", NodeAddr: "192.168.1.2"}
    41  		err = post(chain, &req2, "172.3.4.2", now)
    42  		So(err, ShouldBeNil)
    43  
    44  		wayback := now.Add(-holo.BootstrapTTL * 2)
    45  		req3 := holo.BSReq{Version: 1, NodeID: "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHz", NodeAddr: "192.168.1.3"}
    46  		err = post(chain, &req3, "172.3.4.3", wayback)
    47  		So(err, ShouldBeNil)
    48  
    49  		result, err := get(chain)
    50  		So(err, ShouldBeNil)
    51  		So(result, ShouldEqual, fmt.Sprintf(`[{"Req":{"Version":1,"NodeID":"QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx","NodeAddr":"192.168.1.1"},"Remote":"172.3.4.1","LastSeen":%v},{"Req":{"Version":1,"NodeID":"QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHy","NodeAddr":"192.168.1.2"},"Remote":"172.3.4.2","LastSeen":%v}]`, jsonTime(then), jsonTime(now)))
    52  	})
    53  
    54  	Convey("it handle same node ID on different chains", t, func() {
    55  		chain1 := "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXa1"
    56  		chain2 := "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXa2"
    57  		node := "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx"
    58  		now := time.Now()
    59  		then := now.Add(-halfOfBoostrapTTL)
    60  		req := holo.BSReq{Version: 1, NodeID: node, NodeAddr: "192.168.1.1"}
    61  		err := post(chain1, &req, "172.3.4.1", now)
    62  		So(err, ShouldBeNil)
    63  		err = post(chain2, &req, "172.3.4.1", then)
    64  		So(err, ShouldBeNil)
    65  
    66  		result, err := get(chain1)
    67  		So(err, ShouldBeNil)
    68  		So(result, ShouldEqual, fmt.Sprintf(`[{"Req":{"Version":1,"NodeID":"QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx","NodeAddr":"192.168.1.1"},"Remote":"172.3.4.1","LastSeen":%v}]`, jsonTime(now)))
    69  
    70  		result, err = get(chain2)
    71  		So(err, ShouldBeNil)
    72  		So(result, ShouldEqual, fmt.Sprintf(`[{"Req":{"Version":1,"NodeID":"QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx","NodeAddr":"192.168.1.1"},"Remote":"172.3.4.1","LastSeen":%v}]`, jsonTime(then)))
    73  
    74  	})
    75  }
    76  
    77  func jsonTime(t time.Time) string {
    78  	b, _ := json.Marshal(t)
    79  	return string(b)
    80  }