<commit_before>package restapiserver

import (
	"bytes"
	"encoding/json"
	"github.com/blang/receptor/discovery"
	"github.com/blang/receptor/event"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestFunc(t *testing.T) {
	watcher := discovery.Watchers["restapiserver"].(*RestAPIServerWatcher)
	cfg := &Config{
		Listen: "127.0.0.1:9991",
	}
	b, err := json.Marshal(cfg)
	if err != nil {
		t.Fatalf("Could not marshal cfg: %s", string(b))
	}

	err = watcher.Setup(json.RawMessage(b))
	if err != nil {
		t.Fatalf("Watcher setup failed: %s", err)
	}

	serviceConfig := &ServiceConfig{
		Service: "testservice",
	}
	b, err = json.Marshal(serviceConfig)
	if err != nil {
		t.Fatalf("Could not marshal service cfg: %s", string(b))
	}

	handle, err := watcher.Accept(json.RawMessage(b))
	if err != nil {
		t.Fatalf("Watcher accept failed: %s", err)
	}

	eventCh := make(chan event.Event, 1)
	closeCh := make(chan struct{})

	watcher.IsRunning = true // Fake Running server
	testserver := httptest.NewServer(watcher.Router)
	defer testserver.Close()
	go handle.Handle(eventCh, closeCh)

	restEvent := &RestEvent{
		Name: "testservice",
		Type: "nodeup",
		Host: "127.0.0.1",
		Port: 9991,
	}
	b, err = json.Marshal(restEvent)
	if err != nil {
		t.Fatalf("Could not marshal test restevent: %s", string(b))
	}
	br := bytes.NewReader(b)
	resp, err := http.Post(testserver.URL+"/service/testservice", "application/json", br)
	if err != nil {
		t.Fatalf("Error while Post: %s", err)
	}
	if resp.StatusCode != 200 {
		t.Errorf("RestAPI send wrong statuscode: %d", resp.StatusCode)
	}

	// Check if event was fired
	var recv event.Event
	select {
	case recv = <-eventCh:
	case <-time.After(5 * time.Second):
		t.Fatal("Timeout: No event received")
	}

	// Test if event is correct
	if nlen := len(recv.Nodes()); nlen != 1 {
		t.Errorf("Event has %d nodes, expected 1", nlen)
	}
	if evName := recv.Nodes()[0].Name(); evName != restEvent.Name {
		t.Errorf("Event name was %s, expected %s", evName, restEvent.Name)
	}
	if evHost := recv.Nodes()[0].Host(); evHost != restEvent.Host {
		t.Errorf("Event host was %s, expected %s", evHost, restEvent.Host)
	}
	if evPort := recv.Nodes()[0].Port(); evPort != restEvent.Port {
		t.Errorf("Event port was %s, expected %s", evPort, restEvent.Port)
	}
	if evType := recv.Nodes()[0].Type(); evType != event.EventNodeUp {
		t.Errorf("Event type was %s, expected %s", evType, event.EventNodeUp)
	}

}
<commit_msg>Test shutdown off Restapiserver watcher<commit_after>package restapiserver

import (
	"bytes"
	"encoding/json"
	"github.com/blang/receptor/discovery"
	"github.com/blang/receptor/event"
	"github.com/blang/receptor/handler"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestFunc(t *testing.T) {
	watcher := discovery.Watchers["restapiserver"].(*RestAPIServerWatcher)
	cfg := &Config{
		Listen: "127.0.0.1:9991",
	}
	b, err := json.Marshal(cfg)
	if err != nil {
		t.Fatalf("Could not marshal cfg: %s", string(b))
	}

	err = watcher.Setup(json.RawMessage(b))
	if err != nil {
		t.Fatalf("Watcher setup failed: %s", err)
	}

	serviceConfig := &ServiceConfig{
		Service: "testservice",
	}
	b, err = json.Marshal(serviceConfig)
	if err != nil {
		t.Fatalf("Could not marshal service cfg: %s", string(b))
	}

	handle, err := watcher.Accept(json.RawMessage(b))
	manHandle := handler.NewManagedHandler(handle)
	if err != nil {
		t.Fatalf("Watcher accept failed: %s", err)
	}

	eventCh := make(chan event.Event, 1)

	watcher.IsRunning = true // Fake Running server
	testserver := httptest.NewServer(watcher.Router)
	defer testserver.Close()
	go manHandle.Handle(eventCh)

	restEvent := &RestEvent{
		Name: "testservice",
		Type: "nodeup",
		Host: "127.0.0.1",
		Port: 9991,
	}
	b, err = json.Marshal(restEvent)
	if err != nil {
		t.Fatalf("Could not marshal test restevent: %s", string(b))
	}
	br := bytes.NewReader(b)
	resp, err := http.Post(testserver.URL+"/service/testservice", "application/json", br)
	if err != nil {
		t.Fatalf("Error while Post: %s", err)
	}
	if resp.StatusCode != 200 {
		t.Errorf("RestAPI send wrong statuscode: %d", resp.StatusCode)
	}

	// Check if event was fired
	var recv event.Event
	select {
	case recv = <-eventCh:
	case <-time.After(5 * time.Second):
		t.Fatal("Timeout: No event received")
	}

	// Test if event is correct
	if nlen := len(recv.Nodes()); nlen != 1 {
		t.Errorf("Event has %d nodes, expected 1", nlen)
	}
	if evName := recv.Nodes()[0].Name(); evName != restEvent.Name {
		t.Errorf("Event name was %s, expected %s", evName, restEvent.Name)
	}
	if evHost := recv.Nodes()[0].Host(); evHost != restEvent.Host {
		t.Errorf("Event host was %s, expected %s", evHost, restEvent.Host)
	}
	if evPort := recv.Nodes()[0].Port(); evPort != restEvent.Port {
		t.Errorf("Event port was %s, expected %s", evPort, restEvent.Port)
	}
	if evType := recv.Nodes()[0].Type(); evType != event.EventNodeUp {
		t.Errorf("Event type was %s, expected %s", evType, event.EventNodeUp)
	}

	// Check shutdown
	manHandle.Stop()
	err = manHandle.WaitTimeout(5 * time.Second)
	if err != nil {
		t.Errorf("Stop handle timeout: %s", err)
	}

}

{'fraction_non_alphanumeric': 0.17058362710536623, 'fraction_numerical': 0.012338425381903642, 'mean_word_length': 4.036489151873767, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3505356', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1920, 'n_words': 465}