Skip to content

Commit

Permalink
added connection mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Nov 18, 2017
1 parent d6eab01 commit b7a4cfe
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 10 deletions.
14 changes: 14 additions & 0 deletions network/mock/addr.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/**********************************************************************************
* Copyright (c) 2009-2017 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package mock

// Addr is a fake network interface which implements the net.Addr interface
Expand Down
17 changes: 17 additions & 0 deletions network/mock/addr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mock

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAddr(t *testing.T) {
addr := Addr{
NetworkString: "1",
AddrString: "2",
}

assert.Equal(t, "1", addr.Network())
assert.Equal(t, "2", addr.String())
}
23 changes: 18 additions & 5 deletions network/mock/conn.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/**********************************************************************************
* Copyright (c) 2009-2017 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package mock

import "io"
Expand Down Expand Up @@ -32,10 +46,9 @@ func NewConn() *Conn {
}

// Close closes the mock connection.
func (c *Conn) Close() error {
if err := c.Server.Close(); err != nil {
return err
func (c *Conn) Close() (err error) {
if err = c.Server.Close(); err == nil {
err = c.Client.Close()
}

return c.Client.Close()
return
}
22 changes: 22 additions & 0 deletions network/mock/conn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mock

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConn(t *testing.T) {
conn := NewConn()

buffer := make([]byte, 10)
go func() {
conn.Server.Read(buffer)
}()

n, err := conn.Client.Write([]byte{1})
assert.Equal(t, 1, n)
assert.NoError(t, err)
assert.NoError(t, conn.Close())

}
23 changes: 18 additions & 5 deletions network/mock/end.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/**********************************************************************************
* Copyright (c) 2009-2017 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package mock

import (
Expand All @@ -13,12 +27,11 @@ type End struct {
}

// Close closes the End.
func (e End) Close() error {
if err := e.Writer.Close(); err != nil {
return err
func (e End) Close() (err error) {
if err = e.Writer.Close(); err == nil {
err = e.Reader.Close()
}

return e.Reader.Close()
return
}

// LocalAddr gets the local address.
Expand Down
25 changes: 25 additions & 0 deletions network/mock/end_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mock

import (
"io"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestEnd(t *testing.T) {
r, w := io.Pipe()
end := End{
Reader: r,
Writer: w,
}

assert.Equal(t, "127.0.0.1", end.LocalAddr().String())
assert.Equal(t, "127.0.0.1", end.RemoteAddr().String())
assert.NoError(t, end.Close())
assert.NoError(t, end.SetDeadline(time.Now()))
assert.NoError(t, end.SetReadDeadline(time.Now()))
assert.NoError(t, end.SetWriteDeadline(time.Now()))

}

0 comments on commit b7a4cfe

Please sign in to comment.