Skip to content

Commit

Permalink
完善单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Oct 25, 2016
1 parent f5d926e commit f7b7c5e
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,33 @@ func Test_Async(t *testing.T) {
}

func Test_Channel(t *testing.T) {
channel := NewChannel()
waitClientReady := make(chan struct{})
waitTestDone := make(chan struct{})

channel := NewChannel()
testMessages := make([][]byte, 2000)

waitClientReady := new(sync.WaitGroup)
waitClientReady.Add(60)
go func() {
<-waitClientReady
waitClientReady.Wait()

for i := 0; i < 2000; i++ {
msg := RandBytes(128)
testMessages[i] = msg
channel.Fetch(func(s *Session) {
s.Send(testMessages[i])
})
}

<-waitTestDone

channel.Close()
}()

clientWait := new(sync.WaitGroup)
server, err := Listen("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), 2000)
utest.IsNilNow(t, err)
addr := server.Listener().Addr().String()

go server.Serve(HandlerFunc(func(session *Session) {
defer session.Close()
channel.Put(session.ID(), session)
Expand All @@ -158,12 +165,16 @@ func Test_Channel(t *testing.T) {
utest.EqualNow(t, channel.Get(session.ID()), nil)

channel.Put(session.ID(), session)
clientWait.Done()

waitClientReady.Done()

<-waitTestDone
}))

waitTestFinish := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
clientWait.Add(1)
waitTestFinish.Add(1)

go func() {
session, err := DialTimeout("tcp", addr, time.Second, ProtocolFunc(NewTestCodec), 0)
utest.IsNilNow(t, err)
Expand All @@ -176,12 +187,12 @@ func Test_Channel(t *testing.T) {

session.Close()

close(waitTestDone)
waitTestFinish.Done()
}()
}
clientWait.Wait()
close(waitClientReady)
<-waitTestDone
waitTestFinish.Wait()

close(waitTestDone)

server.Stop()
}
Expand Down

0 comments on commit f7b7c5e

Please sign in to comment.