<commit_before>package test

import (
	"flag"
	"sync"
	"testing"

	"github.com/neptulon/neptulon"
	"github.com/neptulon/neptulon/middleware"
)

var ext = flag.Bool("ext", false, "Run external client test case.")

// Helper method for testing client implementations in other languages.
// Flow of events for this function is:
// * Send a {"method":"echo", "params":{"message": "..."}} request to client upon connection,
//   and verify that message body is echoed properly in the response body.
// * Echo any incoming request message body as is within a response message.
// * Repeat ad infinitum, until {"method":"close", "params":"{"message": "..."}"} is received. Close message body is logged.
func TestExternalClient(t *testing.T) {
	sh := NewServerHelper(t).Start()
	defer sh.CloseWait()
	var wg sync.WaitGroup
	wg.Add(1) // one for response handler below, other for "close" request handler

	// m := "Hello!"

	// sh.Server.ConnHandler(func(c *neptulon.Conn) error {
	// 	c.SendRequest("echo", echoMsg{Message: m}, func(ctx *neptulon.ResCtx) error {
	// 		defer wg.Done()
	// 		var msg echoMsg
	// 		if err := ctx.Result(&msg); err != nil {
	// 			t.Fatal(err)
	// 		}
	// 		if msg.Message != m {
	// 			t.Fatalf("expected: %v got: %v", m, msg.Message)
	// 		}
	// 		return nil
	// 	})
	// 	return nil
	// })

	rout := middleware.NewRouter()
	sh.Middleware(rout.Middleware)
	rout.Request("echo", middleware.Echo)

	rout.Request("close", func(ctx *neptulon.ReqCtx) error {
		defer wg.Done()
		if err := ctx.Params(&ctx.Res); err != nil {
			return err
		}
		err := ctx.Next()
		ctx.Conn.Close()
		t.Logf("Closed connection with message from client: %v\n", ctx.Res)
		return err
	})

	if *ext {
		return
	}

	// use internal conn implementation instead to test the test case itself
	t.Log("Skipping external client integration test since -ext flag is not provided.")
	ch := sh.GetConnHelper().Connect()
	defer ch.CloseWait()
	cm := "Thanks for echoing! Over and out."

	// ch.SendRequest("echo", echoMsg{Message: m}, func(ctx *neptulon.ResCtx) error {
	// 	var msg echoMsg
	// 	if err := ctx.Result(&msg); err != nil {
	// 		t.Fatal(err)
	// 	}
	// 	if msg.Message != m {
	// 		t.Fatalf("expected: %v got: %v", m, msg.Message)
	// 	}
	// 	return nil
	// })

	ch.SendRequest("close", echoMsg{Message: cm}, func(ctx *neptulon.ResCtx) error {
		var msg echoMsg
		if err := ctx.Result(&msg); err != nil {
			t.Fatal(err)
		}
		if msg.Message != cm {<commit_msg>add missing wait for external clients<commit_after>package test

import (
	"flag"
	"sync"
	"testing"

	"github.com/neptulon/neptulon"
	"github.com/neptulon/neptulon/middleware"
)

var ext = flag.Bool("ext", false, "Run external client test case.")

// Helper method for testing client implementations in other languages.
// Flow of events for this function is:
// * Send a {"method":"echo", "params":{"message": "..."}} request to client upon connection,
//   and verify that message body is echoed properly in the response body.
// * Echo any incoming request message body as is within a response message.
// * Repeat ad infinitum, until {"method":"close", "params":"{"message": "..."}"} is received. Close message body is logged.
func TestExternalClient(t *testing.T) {
	sh := NewServerHelper(t).Start()
	defer sh.CloseWait()
	var wg sync.WaitGroup
	wg.Add(1) // one for response handler below, other for "close" request handler

	// m := "Hello!"

	// sh.Server.ConnHandler(func(c *neptulon.Conn) error {
	// 	c.SendRequest("echo", echoMsg{Message: m}, func(ctx *neptulon.ResCtx) error {
	// 		defer wg.Done()
	// 		var msg echoMsg
	// 		if err := ctx.Result(&msg); err != nil {
	// 			t.Fatal(err)
	// 		}
	// 		if msg.Message != m {
	// 			t.Fatalf("expected: %v got: %v", m, msg.Message)
	// 		}
	// 		return nil
	// 	})
	// 	return nil
	// })

	rout := middleware.NewRouter()
	sh.Middleware(rout.Middleware)
	rout.Request("echo", middleware.Echo)

	rout.Request("close", func(ctx *neptulon.ReqCtx) error {
		defer wg.Done()
		if err := ctx.Params(&ctx.Res); err != nil {
			return err
		}
		err := ctx.Next()
		ctx.Conn.Close()
		t.Logf("Closed connection with message from client: %v\n", ctx.Res)
		return err
	})

	if *ext {
		wg.Wait()
		return
	}

	// use internal conn implementation instead to test the test case itself
	t.Log("Skipping external client integration test since -ext flag is not provided.")
	ch := sh.GetConnHelper().Connect()
	defer ch.CloseWait()
	cm := "Thanks for echoing! Over and out."

	// ch.SendRequest("echo", echoMsg{Message: m}, func(ctx *neptulon.ResCtx) error {
	// 	var msg echoMsg
	// 	if err := ctx.Result(&msg); err != nil {
	// 		t.Fatal(err)
	// 	}
	// 	if msg.Message != m {
	// 		t.Fatalf("expected: %v got: %v", m, msg.Message)
	// 	}
	// 	return nil
	// })

	ch.SendRequest("close", echoMsg{Message: cm}, func(ctx *neptulon.ResCtx) error {
		var msg echoMsg
		if err := ctx.Result(&msg); err != nil {
			t.Fatal(err)
		}
		if msg.Message != cm {
{'fraction_non_alphanumeric': 0.16565778853914448, 'fraction_numerical': 0.0004035512510088781, 'mean_word_length': 3.1725589225589226, 'pattern_counts': {'":': 12, '<': 3, '<?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': '96221', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1761, 'n_words': 498}