<commit_before>package nats

import (
	"net"
	"testing"
	"sync"
	"nats/test"
)

type testClient struct {
	// Network pipe
	nc, ns net.Conn

	// Test server
	s *test.TestServer

	// Test client
	c *Client

	// Channel to receive the return value of cl()
	ec chan error

	// WaitGroup to join goroutines after every test
	sync.WaitGroup
}

func (tc *testClient) Setup(t *testing.T) {
	tc.nc, tc.ns = net.Pipe()
	tc.s = test.NewTestServer(t, tc.ns)
	tc.c = NewClient()
	tc.ec = make(chan error, 1)

	tc.Add(1)
	go func() {
		tc.ec <- tc.c.Run(DumbDialer{tc.nc}, EmptyHandshake)
		tc.Done()
	}()
}

func (tc *testClient) Teardown() {
	// Close test server
	tc.s.Close()

	// Wait for goroutines
	tc.Wait()
}

func TestClientCloseInboxOnStop(t *testing.T) {
	var tc testClient

	tc.Setup(t)

	tc.Add(1)
	go func() {
		sub := tc.c.NewSubscription("subject")
		sub.Subscribe()

		_, ok := <-sub.Inbox
		if ok {
			t.Errorf("Expected not OK")
		}

		tc.Done()
	}()

	tc.s.AssertRead("SUB subject 1\r\n")

	// Stop client
	tc.c.Stop()

	// Wait before closing server connection to avoid a race with EOF
	tc.Wait()

	tc.Teardown()
}

func TestClientSubscriptionReceivesMessage(t *testing.T) {
	var tc testClient

	tc.Setup(t)

	tc.Add(1)
	go func() {
		sub := tc.c.NewSubscription("subject")
		sub.Subscribe()

		m := <-sub.Inbox

		expected := "payload"
		actual := string(m.Payload)

		if actual != expected {
			t.Errorf("Expected: %#v, got: %#v", expected, actual)
		}

		tc.Done()
	}()

	tc.s.AssertRead("SUB subject 1\r\n")
	tc.s.AssertWrite("MSG subject 1 7\r\npayload\r\n")
<commit_msg>Call Stop() from test teardown<commit_after>package nats

import (
	"net"
	"testing"
	"sync"
	"nats/test"
)

type testClient struct {
	// Network pipe
	nc, ns net.Conn

	// Test server
	s *test.TestServer

	// Test client
	c *Client

	// Channel to receive the return value of cl()
	ec chan error

	// WaitGroup to join goroutines after every test
	sync.WaitGroup
}

func (tc *testClient) Setup(t *testing.T) {
	tc.nc, tc.ns = net.Pipe()
	tc.s = test.NewTestServer(t, tc.ns)
	tc.c = NewClient()
	tc.ec = make(chan error, 1)

	tc.Add(1)
	go func() {
		tc.ec <- tc.c.Run(DumbDialer{tc.nc}, EmptyHandshake)
		tc.Done()
	}()
}

func (tc *testClient) Teardown() {
	tc.c.Stop()
	tc.Wait()
}

func TestClientCloseInboxOnStop(t *testing.T) {
	var tc testClient

	tc.Setup(t)

	tc.Add(1)
	go func() {
		sub := tc.c.NewSubscription("subject")
		sub.Subscribe()

		_, ok := <-sub.Inbox
		if ok {
			t.Errorf("Expected not OK")
		}

		tc.Done()
	}()

	tc.s.AssertRead("SUB subject 1\r\n")

	tc.Teardown()
}

func TestClientSubscriptionReceivesMessage(t *testing.T) {
	var tc testClient

	tc.Setup(t)

	tc.Add(1)
	go func() {
		sub := tc.c.NewSubscription("subject")
		sub.Subscribe()

		m := <-sub.Inbox

		expected := "payload"
		actual := string(m.Payload)

		if actual != expected {
			t.Errorf("Expected: %#v, got: %#v", expected, actual)
		}

		tc.Done()
	}()

	tc.s.AssertRead("SUB subject 1\r\n")
	tc.s.AssertWrite("MSG subject 1 7\r\npayload\r\n")

{'fraction_non_alphanumeric': 0.1641347424042272, 'fraction_numerical': 0.005284015852047556, 'mean_word_length': 3.149315068493151, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1722312', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1235, 'n_words': 291}