Skip to content

Instantly share code, notes, and snippets.

@rfc1459
Last active December 12, 2015 05:28
Show Gist options
  • Select an option

  • Save rfc1459/4722262 to your computer and use it in GitHub Desktop.

Select an option

Save rfc1459/4722262 to your computer and use it in GitHub Desktop.
Tests for Moebius network I/O loop
/*
* network_test.go - network I/O testing
* Copyright (C) 2013 Matteo Panella <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package moebius
import (
"bufio"
. "launchpad.net/gocheck"
"net"
"strings"
"time"
)
type NetworkSuite struct{}
var _ = Suite(&NetworkSuite{})
func (n *NetworkSuite) TestReadCapability(c *C) {
traffic := []string{"Line 1", "Line 2", "Line 3"}
myEnd, itsEnd := net.Pipe()
fromNetwork := make(chan string)
u := newUplinkFromConn(itsEnd, fromNetwork)
err := u.Connect()
c.Assert(err, IsNil)
myEnd.Write([]byte(strings.Join(traffic, "\n") + "\n"))
for _, msg := range traffic {
select {
case incoming := <-fromNetwork:
c.Assert(incoming, Equals, msg)
case <-time.After(3 * time.Second):
c.FailNow()
}
}
myEnd.Close()
}
func (n *NetworkSuite) TestWriteCapability(c *C) {
refTraffic := []string{"Line 1", "Line 2", "Line 3", "4th line"}
myEnd, itsEnd := net.Pipe()
fromNetwork := make(chan string)
reader := bufio.NewReader(myEnd)
u := newUplinkFromConn(itsEnd, fromNetwork)
err := u.Connect()
c.Assert(err, IsNil)
for _, msg := range refTraffic {
u.Send(msg)
go func() {
data, err := reader.ReadBytes('\n')
c.Assert(err, IsNil)
incoming := toUnicode(data)
c.Assert(incoming, Equals, msg)
}()
}
myEnd.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment