<commit_before>package receiver

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/Nitro/sidecar/catalog"
	"github.com/Nitro/sidecar/service"
	"github.com/mohae/deepcopy"
	. "github.com/smartystreets/goconvey/convey"
)

func Test_updateHandler(t *testing.T) {
	Convey("updateHandler()", t, func() {
		rcvr := &Receiver{
			ReloadChan: make(chan time.Time, 10),
		}
		rcvr.OnUpdate = func(state *catalog.ServicesState) { rcvr.EnqueueUpdate() }

		hostname := "chaucer"
		state := catalog.NewServicesState()
		state.Servers[hostname] = catalog.NewServer(hostname)

		baseTime := time.Now().UTC()

		svcId := "deadbeef123"
		svcId2 := "deadbeef456"

		svc := service.Service{
			ID:       svcId,
			Name:     "bocaccio",
			Image:    "101deadbeef",
			Created:  baseTime,
			Hostname: hostname,
			Updated:  baseTime,
			Status:   service.ALIVE,
		}

		svc2 := service.Service{
			ID:       svcId2,
			Name:     "shakespeare",
			Image:    "202deadbeef",
			Created:  baseTime,
			Hostname: hostname,
			Updated:  baseTime,
			Status:   service.ALIVE,
		}

		state.AddServiceEntry(svc)
		state.AddServiceEntry(svc2)

		req := httptest.NewRequest("POST", "/update", nil)
		recorder := httptest.NewRecorder()

		rcvr.CurrentState = state

		Convey("returns an error on an invalid JSON body", func() {
			UpdateHandler(recorder, req, rcvr)

			resp := recorder.Result()
			So(resp.StatusCode, ShouldEqual, 500)

			bodyBytes, _ := ioutil.ReadAll(resp.Body)
			So(string(bodyBytes), ShouldContainSubstring, "unexpected end of JSON input")
		})

		Convey("updates the state", func() {
			startTime := rcvr.CurrentState.LastChanged

			var evtState *catalog.ServicesState
			evtState = deepcopy.Copy(state).(*catalog.ServicesState)
			evtState.LastChanged = time.Now().UTC()

			change := catalog.StateChangedEvent{
				State: *evtState,
				ChangeEvent: catalog.ChangeEvent{
					Service: service.Service{
						ID:      "10101010101",
						Updated: time.Now().UTC(),
						Status:  service.ALIVE,
					},
					PreviousStatus: service.TOMBSTONE,
				},
			}

			encoded, _ := json.Marshal(change)<commit_msg>Fix test that doesn't pass on 1.7 only<commit_after>package receiver

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/Nitro/sidecar/catalog"
	"github.com/Nitro/sidecar/service"
	"github.com/mohae/deepcopy"
	. "github.com/smartystreets/goconvey/convey"
)

func Test_updateHandler(t *testing.T) {
	Convey("updateHandler()", t, func() {
		rcvr := &Receiver{
			ReloadChan: make(chan time.Time, 10),
		}
		rcvr.OnUpdate = func(state *catalog.ServicesState) { rcvr.EnqueueUpdate() }

		hostname := "chaucer"
		state := catalog.NewServicesState()
		state.Servers[hostname] = catalog.NewServer(hostname)

		baseTime := time.Now().UTC()

		svcId := "deadbeef123"
		svcId2 := "deadbeef456"

		svc := service.Service{
			ID:       svcId,
			Name:     "bocaccio",
			Image:    "101deadbeef",
			Created:  baseTime,
			Hostname: hostname,
			Updated:  baseTime,
			Status:   service.ALIVE,
		}

		svc2 := service.Service{
			ID:       svcId2,
			Name:     "shakespeare",
			Image:    "202deadbeef",
			Created:  baseTime,
			Hostname: hostname,
			Updated:  baseTime,
			Status:   service.ALIVE,
		}

		state.AddServiceEntry(svc)
		state.AddServiceEntry(svc2)

		req := httptest.NewRequest("POST", "/update", nil)
		recorder := httptest.NewRecorder()

		rcvr.CurrentState = state

		Convey("returns an error on an invalid JSON body", func() {
			UpdateHandler(recorder, req, rcvr)

			resp := recorder.Result()
			So(resp.StatusCode, ShouldEqual, 500)

			bodyBytes, _ := ioutil.ReadAll(resp.Body)
			So(string(bodyBytes), ShouldContainSubstring, "unexpected end of JSON input")
		})

		Convey("updates the state", func() {
			startTime := rcvr.CurrentState.LastChanged

			var evtState *catalog.ServicesState
			evtState = deepcopy.Copy(state).(*catalog.ServicesState)
			evtState.LastChanged = time.Now().UTC()

			change := catalog.StateChangedEvent{
				State: *evtState,
				ChangeEvent: catalog.ChangeEvent{
					Service: service.Service{
						ID:      "10101010101",
						Updated: time.Now().UTC(),
						Created: time.Now().UTC(),
						Status:  service.ALIVE,
					},
					PreviousStatus: service.TOMBSTONE,
				},
			}

			encoded, _ := json.Marshal(change)
{'fraction_non_alphanumeric': 0.1384051329055912, 'fraction_numerical': 0.015123739688359304, 'mean_word_length': 3.5046439628482973, 'pattern_counts': {'":': 0, '<': 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': '771980', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1549, 'n_words': 288}