Use a channel to safely synchronize datachannel writes, (#12)

clean up ice candidate log message.
still need to debug the copy loop break.
This commit is contained in:
Serene Han 2016-02-19 16:17:17 -08:00
parent c4215b5614
commit c3ada1b545
2 changed files with 47 additions and 18 deletions

View file

@ -9,10 +9,12 @@ import (
type MockDataChannel struct {
destination bytes.Buffer
done chan bool
}
func (m *MockDataChannel) Send(data []byte) {
m.destination.Write(data)
m.done <- true
}
func (*MockDataChannel) Close() error {
@ -24,6 +26,7 @@ func TestConnect(t *testing.T) {
Convey("WebRTC Connection", func() {
c := new(webRTCConn)
c.BytesInfo = &BytesInfo{
inboundChan: make(chan int), outboundChan: make(chan int),
inbound: 0, outbound: 0, inEvents: 0, outEvents: 0,
@ -31,15 +34,19 @@ func TestConnect(t *testing.T) {
So(c.buffer.Bytes(), ShouldEqual, nil)
Convey("SendData buffers when datachannel is nil", func() {
c.sendData([]byte("test"))
c.SendData([]byte("test"))
c.snowflake = nil
So(c.buffer.Bytes(), ShouldResemble, []byte("test"))
})
Convey("SendData sends to datachannel when not nil", func() {
mock := new(MockDataChannel)
mock.done = make(chan bool)
go c.SendLoop()
c.writeChannel = make(chan []byte)
c.snowflake = mock
c.sendData([]byte("test"))
c.SendData([]byte("test"))
<-mock.done
So(c.buffer.Bytes(), ShouldEqual, nil)
So(mock.destination.Bytes(), ShouldResemble, []byte("test"))
})