Skip to content

Commit

Permalink
add net case
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Jun 11, 2024
1 parent b496d10 commit a178360
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions net_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"net"
"sync"
"testing"
"time"

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

func TestServer(t *testing.T) {
assert := assert.New(t)
testCount := 100

t.Run("serve", func(t *testing.T) {
fd, err := TcpServer(20083)
assert.Nil(err)

// start listener
go func() {
for {
cfd, err := Accept(fd)
assert.Nil(err)

// read
var buf [32]byte
n, _ := Read(cfd, buf[:])

// write
Write(cfd, buf[:n])
}
}()

var wg sync.WaitGroup

// start clients
go func() {
for i := 0; i < testCount; i++ {
tcpConn, err := net.Dial("tcp", "127.0.0.1:20083")
assert.Nil(err)
wg.Add(1)

// write
msg := fmt.Sprintf("%d", time.Now().UnixNano())
tcpConn.Write([]byte(msg))

// read
var res [32]byte
n, err := tcpConn.Read(res[:])
assert.Nil(err)

// equal
assert.Equal(msg, string(res[:n]))
wg.Done()
}
}()

wg.Wait()

time.Sleep(time.Second)
})
}

0 comments on commit a178360

Please sign in to comment.