68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
// Copyright © 2023 Roberto Hidalgo <event-gateway@un.rob.mx>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package consul_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.rob.mx/nidito/event-gateway/internal/config"
|
|
"git.rob.mx/nidito/event-gateway/internal/sink/debug"
|
|
"git.rob.mx/nidito/event-gateway/internal/source/consul"
|
|
"git.rob.mx/nidito/event-gateway/internal/source/types"
|
|
"github.com/hashicorp/consul/api"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type mockConsul struct {
|
|
calls []string
|
|
}
|
|
|
|
func (mc *mockConsul) List(name string, q *api.QueryOptions) ([]*api.UserEvent, *api.QueryMeta, error) {
|
|
time.Sleep(10 * time.Millisecond)
|
|
mc.calls = append(mc.calls, name)
|
|
return []*api.UserEvent{}, &api.QueryMeta{LastIndex: 100}, nil
|
|
}
|
|
|
|
func TestConsulReloads(t *testing.T) {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
client := &mockConsul{}
|
|
|
|
c := &consul.Source{Client: client}
|
|
c.Initialize()
|
|
|
|
if len(client.calls) != 0 {
|
|
t.Fatalf("empty consul fetched events: %+v", client.calls)
|
|
}
|
|
|
|
target := &debug.Event{}
|
|
l := &config.Listener{
|
|
ID: "some-event-name",
|
|
Source: &config.RawSource{
|
|
Kind: types.Consul,
|
|
Config: []byte(`{}`),
|
|
},
|
|
Event: target,
|
|
}
|
|
|
|
if err := c.Register(l); err != nil {
|
|
t.Fatalf("could not register source: %s", err)
|
|
}
|
|
time.Sleep(15 * time.Millisecond)
|
|
if len(client.calls) == 0 {
|
|
t.Fatalf("consul did not fetch events: %+v", client.calls)
|
|
}
|
|
|
|
had := len(client.calls)
|
|
c.Deregister(l.ID)
|
|
|
|
afterClear := append([]string{}, client.calls...)
|
|
if len(afterClear) > had {
|
|
t.Fatalf("consul continued fetching events (%d > %d): %+v", len(afterClear), had, afterClear)
|
|
}
|
|
|
|
if len(afterClear) != len(client.calls) {
|
|
t.Fatalf("continued fetching after clear and run: %+v", client.calls)
|
|
}
|
|
}
|