Created
September 16, 2019 09:27
-
-
Save acidsound/91b2fda10dddbc4cf25f2ad2a4bf4dee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"net" | |
"os" | |
"os/signal" | |
"syscall" | |
"testing" | |
"time" | |
) | |
func server(errCh chan error, termCh chan bool) { | |
l, err := net.Listen("tcp", "localhost:4242") | |
if err != nil { | |
errCh <- err | |
return | |
} | |
conn, err := l.Accept() | |
if err != nil { | |
errCh <- err | |
return | |
} | |
bufio := bufio.NewReader(conn) | |
defer func() { | |
fmt.Println("end of server") | |
close(termCh) | |
}() | |
cnt := 0 | |
for { | |
buf, err := bufio.ReadBytes(0x0A) | |
cnt ++ | |
fmt.Println(cnt, len(buf), "bytes read") | |
if err != nil { | |
if err != io.EOF { | |
errCh <- err | |
} | |
break | |
} | |
} | |
} | |
func TestCheck(t *testing.T) { | |
errChan := make(chan error) | |
signChan := make(chan os.Signal, 1) | |
termChan := make(chan bool) | |
signal.Notify(signChan, syscall.SIGINT, syscall.SIGTERM) | |
go server(errChan, termChan) | |
time.AfterFunc(2*time.Second, func() { | |
fmt.Println("timeout") | |
termChan <- true | |
}) | |
conn, err := net.Dial("tcp", "localhost:4242") | |
if err != nil { | |
t.Error(err) | |
} | |
count := 600 | |
for i:=count-1; i>0; i-- { | |
n, err := conn.Write([]byte("12345678901234567890123456789012345678901234567890\n")) | |
if err != nil { | |
t.Error(err) | |
} | |
fmt.Println(n, "bytes written") | |
} | |
conn.Close() | |
select { | |
case <-signChan: | |
case <-termChan: | |
} | |
fmt.Println("server ended") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment