|
|
|
@ -1,9 +1,15 @@ |
|
|
|
|
package p2p |
|
|
|
|
package p2p_test |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bufio" |
|
|
|
|
"bytes" |
|
|
|
|
"encoding/gob" |
|
|
|
|
"net" |
|
|
|
|
"reflect" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/simple-rules/harmony-benchmark/blockchain" |
|
|
|
|
"github.com/simple-rules/harmony-benchmark/p2p" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func setUpTestServer(times int, t *testing.T, conCreated chan struct{}) { |
|
|
|
@ -18,11 +24,11 @@ func setUpTestServer(times int, t *testing.T, conCreated chan struct{}) { |
|
|
|
|
) |
|
|
|
|
for times > 0 { |
|
|
|
|
times-- |
|
|
|
|
data, err := ReadMessageContent(conn) |
|
|
|
|
data, err := p2p.ReadMessageContent(conn) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("error when ReadMessageContent %v", err) |
|
|
|
|
} |
|
|
|
|
data = CreateMessage(byte(1), data) |
|
|
|
|
data = p2p.CreateMessage(byte(1), data) |
|
|
|
|
w.Write(data) |
|
|
|
|
w.Flush() |
|
|
|
|
} |
|
|
|
@ -40,9 +46,9 @@ func TestNewNewNode(t *testing.T) { |
|
|
|
|
times-- |
|
|
|
|
|
|
|
|
|
myMsg := "minhdoan" |
|
|
|
|
SendMessageContent(conn, []byte(myMsg)) |
|
|
|
|
p2p.SendMessageContent(conn, []byte(myMsg)) |
|
|
|
|
|
|
|
|
|
data, err := ReadMessageContent(conn) |
|
|
|
|
data, err := p2p.ReadMessageContent(conn) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error("got an error when trying to receive an expected message from server.") |
|
|
|
|
} |
|
|
|
@ -51,3 +57,20 @@ func TestNewNewNode(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestGobEncode(t *testing.T) { |
|
|
|
|
block := blockchain.CreateTestingGenesisBlock() |
|
|
|
|
|
|
|
|
|
var tmp bytes.Buffer |
|
|
|
|
enc := gob.NewEncoder(&tmp) |
|
|
|
|
enc.Encode(*block) |
|
|
|
|
|
|
|
|
|
tmp2 := bytes.NewBuffer(tmp.Bytes()) |
|
|
|
|
dec := gob.NewDecoder(tmp2) |
|
|
|
|
|
|
|
|
|
var block2 blockchain.Block |
|
|
|
|
dec.Decode(&block2) |
|
|
|
|
if !reflect.DeepEqual(*block, block2) { |
|
|
|
|
t.Error("Error in GobEncode") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|