<commit_before>// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gtcp_test

import (
	"fmt"
	"github.com/gogf/gf/net/gtcp"
	"github.com/gogf/gf/test/gtest"
	"testing"
	"time"
)

func Test_Pool_Basic1(t *testing.T) {
	p := ports.PopRand()
	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
		defer conn.Close()
		for {
			data, err := conn.RecvPkg()
			if err != nil {
				break
			}
			conn.SendPkg(data)
		}
	})
	go s.Run()
	defer func() {
		s.Close()
	}()
	time.Sleep(100 * time.Millisecond)
	gtest.Case(t, func() {
		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
		gtest.Assert(err, nil)
		defer conn.Close()
		data := []byte("9999")
		err = conn.SendPkg(data)
		gtest.Assert(err, nil)
		err = conn.SendPkgWithTimeout(data, time.Second)
		gtest.Assert(err, nil)
	})
}

func Test_Pool_Basic2(t *testing.T) {
	p := ports.PopRand()
	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
		conn.Close()
	})
	go s.Run()
	defer func() {
		s.Close()
	}()
	time.Sleep(100 * time.Millisecond)
	gtest.Case(t, func() {
		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
		gtest.Assert(err, nil)
		defer conn.Close()
		data := []byte("9999")
		err = conn.SendPkg(data)
		gtest.Assert(err, nil)
		//err = conn.SendPkgWithTimeout(data, time.Second)
		//gtest.Assert(err, nil)

		_, err = conn.SendRecv(data, -1)
		gtest.AssertNE(err, nil)
	})
}
<commit_msg>fix data race issue in unit testing of gtcp<commit_after>// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gtcp_test

import (
	"fmt"
	"github.com/gogf/gf/net/gtcp"
	"github.com/gogf/gf/test/gtest"
	"testing"
	"time"
)

func Test_Pool_Basic1(t *testing.T) {
	p := ports.PopRand()
	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
		defer conn.Close()
		for {
			data, err := conn.RecvPkg()
			if err != nil {
				break
			}
			conn.SendPkg(data)
		}
	})
	go s.Run()
	defer s.Close()
	time.Sleep(500 * time.Millisecond)
	gtest.Case(t, func() {
		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
		gtest.Assert(err, nil)
		defer conn.Close()
		data := []byte("9999")
		err = conn.SendPkg(data)
		gtest.Assert(err, nil)
		err = conn.SendPkgWithTimeout(data, time.Second)
		gtest.Assert(err, nil)
	})
}

func Test_Pool_Basic2(t *testing.T) {
	p := ports.PopRand()
	s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
		conn.Close()
	})
	go s.Run()
	defer s.Close()
	time.Sleep(500 * time.Millisecond)
	gtest.Case(t, func() {
		conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
		gtest.Assert(err, nil)
		defer conn.Close()
		data := []byte("9999")
		err = conn.SendPkg(data)
		gtest.Assert(err, nil)
		//err = conn.SendPkgWithTimeout(data, time.Second)
		//gtest.Assert(err, nil)

		_, err = conn.SendRecv(data, -1)
		gtest.AssertNE(err, nil)
	})
}

{'fraction_non_alphanumeric': 0.1820418204182042, 'fraction_numerical': 0.02029520295202952, 'mean_word_length': 3.4745529573590095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7126512', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1335, 'n_words': 286}