<commit_before>package turms

import (
	"golang.org/x/net/context"
	"log"
	"sync"
	"testing"
)

const (
	realmName = "test"
)

type logHandler struct {
	logf func(format string, v ...interface{})
}

func (h *logHandler) Handle(ctx context.Context, c Conn, msg Message) context.Context {
	se, ok := SessionFromContext(ctx)
	if !ok {
		h.logf("[DEBUG][UnknownSession]: %#v", msg)
		return ctx
	}
	h.logf("[DEBUG][%d]: %#v", se.ID(), msg)
	return ctx
}

func TestRouterBasicProfile(t *testing.T) {
	router := NewRouter()

	verbose := true
	if verbose {
		router.Handler = &Chain{
			Realm(realmName),
			&logHandler{
				log.Printf,
			},
			&Chain{
				Broker(),
				Dealer(),
			},
		}
	} else {
		router.Handler = &Chain{
			Realm(realmName),
			&Chain{
				Broker(),
				Dealer(),
			},
		}
	}

	// Subscribe & Publish
	publisher := router.Client()
	err := publisher.JoinRealm(context.Background(), realmName, &ClientDetails{Publisher: true})
	if err != nil {
		t.Error(err)
	}

	subscriber := router.Client()
	err = subscriber.JoinRealm(context.Background(), realmName, &ClientDetails{Subscriber: true})
	if err != nil {
		t.Error(err)
	}

	sub, err := subscriber.Subscribe(context.Background(), "test")
	if err != nil {
		t.Error(err)
	}

	eventText := "Hello"
	err = publisher.Publish(context.Background(), &PublishOption{true}, "test", []interface{}{eventText}, nil)
	if err != nil {
		t.Error(err)
	}

	event := <-sub
	if len(event.Args) == 0 {
		t.Errorf("Received event %v should contain one argument", event)
	} else {
		text, ok := event.Args[0].(string)
		if !ok {
			t.Errorf("Expected argument 0 to be of type string, got %T", event.Args[0])
		}
		if text != eventText {
			t.Errorf("Expected argument 0 to be %s, got %s", eventText, text)
		}
	}

	err = publisher.Close()
	if err != nil {
		t.Error(err)
	}

	err = subscriber.Close()
	if err != nil {
		t.Error(err)
	}

	// Register & Call
	callee := router.Client()
	err = callee.JoinRealm(context.Background(), realmName, &ClientDetails{Callee: true})
	if err != nil {
		t.Error(err)
	}

	caller := router.Client()
	err = caller.JoinRealm(context.Background(), realmName, &ClientDetails{Caller: true})
	if err != nil {
		t.Error(err)
	}

	procedureName := URI("com.test")
	helloCallee := "Hello Callee"
	helloCaller := "Hello Caller"
	procedureArgs := []interface{}{helloCallee}
	procedureArgsKW := map[string]interface{}{"test": "value"}
	err = callee.Register(context.Background(), procedureName, func(ctx context.Context, inv *Invocation) ([]interface{}, map[string]interface{}, error) {
		arg := inv.Args[0].(string)
		if arg != helloCallee {
			t.Errorf("Expected args[0] to be %s got %s", helloCallee, arg)
		}
		kw := inv.ArgsKW["test"].(string)
		if kw != "value" {
			t.Errorf("Expected argsKW[\"test\"] to be %s got %s", "value", arg)
		}
		return []interface{}{helloCaller}, nil, nil
	})
	if err != nil {
		t.Error(err)
	}

	res, err := caller.Call(context.Background(), procedureName, procedureArgs, procedureArgsKW)
	if err != nil {
		t.Error(err)
	}
	if res.Args[0].(string) != helloCaller {
		t.Errorf("Expected res.Args[0] to be %s got %s", helloCaller, res.Args[0].(string))
	}

	err = router.Close()
	if err != nil {
		t.Error(err)
	}
}

func TestRouterMultipleClients(t *testing.T) {
	router := NewRouter()
	defer func() {
		err := router.Close()
		if err != nil {
			t.Error(err)<commit_msg>Test multiple router close calls.<commit_after>package turms

import (
	"golang.org/x/net/context"
	"log"
	"sync"
	"testing"
)

const (
	realmName = "test"
)

type logHandler struct {
	logf func(format string, v ...interface{})
}

func (h *logHandler) Handle(ctx context.Context, c Conn, msg Message) context.Context {
	se, ok := SessionFromContext(ctx)
	if !ok {
		h.logf("[DEBUG][UnknownSession]: %#v", msg)
		return ctx
	}
	h.logf("[DEBUG][%d]: %#v", se.ID(), msg)
	return ctx
}

func TestRouterBasicProfile(t *testing.T) {
	router := NewRouter()

	verbose := true
	if verbose {
		router.Handler = &Chain{
			Realm(realmName),
			&logHandler{
				log.Printf,
			},
			&Chain{
				Broker(),
				Dealer(),
			},
		}
	} else {
		router.Handler = &Chain{
			Realm(realmName),
			&Chain{
				Broker(),
				Dealer(),
			},
		}
	}

	// Subscribe & Publish
	publisher := router.Client()
	err := publisher.JoinRealm(context.Background(), realmName, &ClientDetails{Publisher: true})
	if err != nil {
		t.Error(err)
	}

	subscriber := router.Client()
	err = subscriber.JoinRealm(context.Background(), realmName, &ClientDetails{Subscriber: true})
	if err != nil {
		t.Error(err)
	}

	sub, err := subscriber.Subscribe(context.Background(), "test")
	if err != nil {
		t.Error(err)
	}

	eventText := "Hello"
	err = publisher.Publish(context.Background(), &PublishOption{true}, "test", []interface{}{eventText}, nil)
	if err != nil {
		t.Error(err)
	}

	event := <-sub
	if len(event.Args) == 0 {
		t.Errorf("Received event %v should contain one argument", event)
	} else {
		text, ok := event.Args[0].(string)
		if !ok {
			t.Errorf("Expected argument 0 to be of type string, got %T", event.Args[0])
		}
		if text != eventText {
			t.Errorf("Expected argument 0 to be %s, got %s", eventText, text)
		}
	}

	err = publisher.Close()
	if err != nil {
		t.Error(err)
	}

	err = subscriber.Close()
	if err != nil {
		t.Error(err)
	}

	// Register & Call
	callee := router.Client()
	err = callee.JoinRealm(context.Background(), realmName, &ClientDetails{Callee: true})
	if err != nil {
		t.Error(err)
	}

	caller := router.Client()
	err = caller.JoinRealm(context.Background(), realmName, &ClientDetails{Caller: true})
	if err != nil {
		t.Error(err)
	}

	procedureName := URI("com.test")
	helloCallee := "<NAME>"
	helloCaller := "<NAME>"
	procedureArgs := []interface{}{helloCallee}
	procedureArgsKW := map[string]interface{}{"test": "value"}
	err = callee.Register(context.Background(), procedureName, func(ctx context.Context, inv *Invocation) ([]interface{}, map[string]interface{}, error) {
		arg := inv.Args[0].(string)
		if arg != helloCallee {
			t.Errorf("Expected args[0] to be %s got %s", helloCallee, arg)
		}
		kw := inv.ArgsKW["test"].(string)
		if kw != "value" {
			t.Errorf("Expected argsKW[\"test\"] to be %s got %s", "value", arg)
		}
		return []interface{}{helloCaller}, nil, nil
	})
	if err != nil {
		t.Error(err)
	}

	res, err := caller.Call(context.Background(), procedureName, procedureArgs, procedureArgsKW)
	if err != nil {
		t.Error(err)
	}
	if res.Args[0].(string) != helloCaller {
		t.Errorf("Expected res.Args[0] to be %s got %s", helloCaller, res.Args[0].(string))
	}

	err = router.Close()
	if err != nil {
		t.Error(err)
	}

	err = router.Close()
	if err == nil {
		t.Errorf("router.Close should return an error if its already closed")
	}
}

func TestRouterMultipleClients(t *testing.T) {
	router := NewRouter()
	defer func() {
		err := router.Close()
		if err != nil {
			t.Error(err)
{'fraction_non_alphanumeric': 0.175512578159081, 'fraction_numerical': 0.0029082448742184094, 'mean_word_length': 3.7997208653175156, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, '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': '2043102', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2499, 'n_words': 597}