mirror of https://github.com/schollz/croc.git
no isclosed
This commit is contained in:
parent
6b149480d4
commit
a17f3096a0
|
@ -15,21 +15,6 @@ type Comm struct {
|
||||||
connection net.Conn
|
connection net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Comm) IsClosed() bool {
|
|
||||||
one := []byte{}
|
|
||||||
c.connection.SetReadDeadline(time.Now())
|
|
||||||
_, err := c.connection.Read(one)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
c.connection.Close()
|
|
||||||
c.connection = nil
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
c.connection.SetReadDeadline(time.Now().Add(3 * time.Hour))
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewConnection gets a new comm to a tcp address
|
// NewConnection gets a new comm to a tcp address
|
||||||
func NewConnection(address string) (c *Comm, err error) {
|
func NewConnection(address string) (c *Comm, err error) {
|
||||||
connection, err := net.DialTimeout("tcp", address, 3*time.Second)
|
connection, err := net.DialTimeout("tcp", address, 3*time.Second)
|
||||||
|
|
|
@ -47,7 +47,7 @@ func TestComm(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
a, err := NewConnection("localhost:" + port)
|
a, err := NewConnection("localhost:" + port)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
data, err := a.Receive()
|
data, err := a.Receive()
|
||||||
|
|
|
@ -17,7 +17,6 @@ func TestTCP(t *testing.T) {
|
||||||
_, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
_, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
assert.False(t, c1.IsClosed())
|
|
||||||
// try sending data
|
// try sending data
|
||||||
assert.Nil(t, c1.Send([]byte("hello, c2")))
|
assert.Nil(t, c1.Send([]byte("hello, c2")))
|
||||||
data, err := c2.Receive()
|
data, err := c2.Receive()
|
||||||
|
@ -30,5 +29,4 @@ func TestTCP(t *testing.T) {
|
||||||
assert.Equal(t, []byte("hello, c1"), data)
|
assert.Equal(t, []byte("hello, c1"), data)
|
||||||
|
|
||||||
c1.Close()
|
c1.Close()
|
||||||
assert.True(t, c1.IsClosed())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ func TestExists(t *testing.T) {
|
||||||
func TestHashFile(t *testing.T) {
|
func TestHashFile(t *testing.T) {
|
||||||
b, err := HashFile("utils.go")
|
b, err := HashFile("utils.go")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "33303e23c9c5e9c194d50b399391754e", fmt.Sprintf("%x", b))
|
assert.Equal(t, "9a66e5c18b9759073666953da376c037", fmt.Sprintf("%x", b))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SHA256 returns sha256 sum
|
// SHA256 returns sha256 sum
|
||||||
|
|
Loading…
Reference in New Issue