|
|
@ -1,9 +1,15 @@ |
|
|
|
package p2p |
|
|
|
package p2p_test |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bufio" |
|
|
|
"bufio" |
|
|
|
|
|
|
|
"bytes" |
|
|
|
|
|
|
|
"encoding/gob" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
|
|
|
|
"reflect" |
|
|
|
"testing" |
|
|
|
"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{}) { |
|
|
|
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 { |
|
|
|
for times > 0 { |
|
|
|
times-- |
|
|
|
times-- |
|
|
|
data, err := ReadMessageContent(conn) |
|
|
|
data, err := p2p.ReadMessageContent(conn) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("error when ReadMessageContent %v", err) |
|
|
|
t.Fatalf("error when ReadMessageContent %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
data = CreateMessage(byte(1), data) |
|
|
|
data = p2p.CreateMessage(byte(1), data) |
|
|
|
w.Write(data) |
|
|
|
w.Write(data) |
|
|
|
w.Flush() |
|
|
|
w.Flush() |
|
|
|
} |
|
|
|
} |
|
|
@ -40,9 +46,9 @@ func TestNewNewNode(t *testing.T) { |
|
|
|
times-- |
|
|
|
times-- |
|
|
|
|
|
|
|
|
|
|
|
myMsg := "minhdoan" |
|
|
|
myMsg := "minhdoan" |
|
|
|
SendMessageContent(conn, []byte(myMsg)) |
|
|
|
p2p.SendMessageContent(conn, []byte(myMsg)) |
|
|
|
|
|
|
|
|
|
|
|
data, err := ReadMessageContent(conn) |
|
|
|
data, err := p2p.ReadMessageContent(conn) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Error("got an error when trying to receive an expected message from server.") |
|
|
|
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") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|