package network

import (
	"log"
	"net"
	"testing"
)

func TestListenTCP(t *testing.T) {
	listener, err := ListenTCP(NetTCP, "0.0.0.0:8899")
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		_ = listener.Close()
	}()
	for {
		conn, err := listener.Accept()
		if err != nil {
			t.Error(err)
			return
		}
		go func(conn net.Conn) {
			defer func() {
				_ = conn.Close()
			}()
			for {
				b := make([]byte, 512)
				n, err := conn.Read(b)
				if err != nil {
					log.Println(err)
					return
				}
				log.Println("Hex:", Bytes(b[:n]).HexTo())
				log.Println(string(b[:n]))
			}
		}(conn)
	}
}
