Skip to content

Commit

Permalink
fixed: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Aug 8, 2017
1 parent f1982a9 commit a5e3cb1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 94 deletions.
2 changes: 2 additions & 0 deletions benchmark/io/io_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build benchmark

package io

import (
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tadpole/logic/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewWorld() *World {

func (w *World) Init() {
nano.OnSessionClosed(func(s *session.Session) {
w.Leave(s.Uid())
w.Leave(s)
w.Broadcast("leave", &protocol.LeaveWorldResponse{ID: s.ID()})
log.Println(fmt.Sprintf("session count: %d", w.Count()))
})
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tadpole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func serve(ctx *cli.Context) error {
nano.SetCheckOriginFunc(func(_ *http.Request) bool { return true })

addr := ctx.String("addr")
nano.Listen(addr, true)
nano.ListenWS(addr)

return nil
}
16 changes: 3 additions & 13 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestChannel_Add(t *testing.T) {
w := make(chan bool, paraCount)
for i := 0; i < paraCount; i++ {
go func(id int) {
s := &session.Session{}
s := session.New(nil)
s.Bind(int64(id + 1))
c.Add(s)
w <- true
Expand All @@ -26,7 +26,7 @@ func TestChannel_Add(t *testing.T) {
}

if c.Count() != paraCount {
t.Fail()
t.Fatalf("count expect: %d, got: %d", paraCount, c.Count())
}

n := rand.Int63n(int64(paraCount) + 1)
Expand All @@ -35,17 +35,7 @@ func TestChannel_Add(t *testing.T) {
}

// leave
for i := 0; i < paraCount; i++ {
go func(id int) {
c.Leave(int64(id) + 1)
w <- true
}(i)
}

for i := 0; i < paraCount; i++ {
<-w
}

c.LeaveAll()
if c.Count() != 0 {
t.Fail()
}
Expand Down
75 changes: 75 additions & 0 deletions internal/codec/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package codec

import (
"reflect"
"testing"

. "github.com/lonnng/nano/internal/packet"
)

func TestPack(t *testing.T) {
data := []byte("hello world")
p1 := &Packet{Type: Handshake, Data: data, Length: len(data)}
pp1, err := Encode(Handshake, data)
if err != nil {
t.Error(err.Error())
}

d1 := NewDecoder()
packets, err := d1.Decode(pp1)
if err != nil {
t.Fatal(err.Error())
}
if len(packets) < 1 {
t.Fatal("packets should not empty")
}
if !reflect.DeepEqual(p1, packets[0]) {
t.Fatalf("expect: %v, got: %v", p1, packets[0])
}

p2 := &Packet{Type: PacketType(5), Data: data, Length: len(data)}
pp2, err := Encode(Kick, data)
if err != nil {
t.Error(err.Error())
}

d2 := NewDecoder()
upp2, err := d2.Decode(pp2)
if err != nil {
t.Fatal(err.Error())
}
if len(upp2) < 1 {
t.Fatal("packets should not empty")
}
if !reflect.DeepEqual(p2, upp2[0]) {
t.Fatalf("expect: %v, got: %v", p2, upp2[0])
}

_ = &Packet{Type: PacketType(0), Data: data, Length: len(data)}
if _, err := Encode(PacketType(0), data); err == nil {
t.Error("should err")
}

_ = &Packet{Type: PacketType(6), Data: data, Length: len(data)}
if _, err = Encode(PacketType(6), data); err == nil {
t.Error("should err")
}

p5 := &Packet{Type: PacketType(5), Data: data, Length: len(data)}
pp5, err := Encode(Kick, data)
if err != nil {
t.Fatal(err.Error())
}
d3 := NewDecoder()
upp5, err := d3.Decode(append(pp5, []byte{0x01, 0x00, 0x00, 0x00}...))
if err != nil {
t.Fatal(err.Error())
}
if len(upp5) < 1 {
t.Fatal("packets should not empty")
}

if !reflect.DeepEqual(p5, upp5[0]) {
t.Fatalf("expect: %v, got: %v", p2, upp5[0])
}
}
67 changes: 0 additions & 67 deletions internal/packet/packet_test.go

This file was deleted.

15 changes: 7 additions & 8 deletions serialize/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Message struct {
func TestSerializer_Serialize(t *testing.T) {
m := Message{1, "hello world"}
s := NewSerializer()
b, err := s.Serialize(m)
b, err := s.Marshal(m)
if err != nil {
t.Fail()
}

m2 := Message{}
if err := s.Deserialize(b, &m2); err != nil {
if err := s.Unmarshal(b, &m2); err != nil {
t.Fail()
}

Expand All @@ -28,13 +28,12 @@ func TestSerializer_Serialize(t *testing.T) {
}
}


func BenchmarkSerializer_Serialize(b *testing.B) {
m := &Message{100, "hell world"}
s := NewSerializer()

for i := 0; i < b.N; i++ {
s.Serialize(m)
s.Marshal(m)
}

b.ReportAllocs()
Expand All @@ -44,13 +43,13 @@ func BenchmarkSerializer_Deserialize(b *testing.B) {
m := &Message{100, "hell world"}
s := NewSerializer()

d, err := s.Serialize(m)
d, err := s.Marshal(m)
if err != nil {
b.Error(err)
}

for i := 0; i<b.N; i++ {
for i := 0; i < b.N; i++ {
m1 := &Message{}
s.Deserialize(d, m1)
s.Unmarshal(d, m1)
}
}
}
4 changes: 2 additions & 2 deletions serialize/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestProtobufSerialezer_Serialize(t *testing.T) {
}

m1 := &testdata.Ping{}
s.Deserialize(b, m1)
s.Unmarshal(b, m1)

if !reflect.DeepEqual(m, m1) {
t.Fail()
Expand Down Expand Up @@ -46,6 +46,6 @@ func BenchmarkSerializer_Deserialize(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m1 := &testdata.Ping{}
s.Deserialize(d, m1)
s.Unmarshal(d, m1)
}
}
4 changes: 2 additions & 2 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestNewSession(t *testing.T) {
s := New(nil)
if s.ID < 1 {
if s.ID() < 1 {
t.Fail()
}
}
Expand All @@ -14,7 +14,7 @@ func TestSession_Bind(t *testing.T) {
uids := []int64{100, 1000, 10000000}
for i, uid := range uids {
s.Bind(uid)
if s.Uid != uids[i] {
if s.Uid() != uids[i] {
t.Fail()
}
}
Expand Down

0 comments on commit a5e3cb1

Please sign in to comment.