Skip to content

Commit

Permalink
add new 'WithErrors' constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkenny committed Apr 17, 2024
1 parent a784368 commit b6040d1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,27 @@ func NewSenderWithNewSiteAndDevice(siteAndDevice *api.SiteAndDeviceCreate, error
return cfg.start(client, d, errors)
}

func NewSenderWithNewSiteAndDeviceWithErrors(siteAndDevice *api.SiteAndDeviceCreate, cfg *Config) (*Sender, <-chan error, error) {
client := cfg.client()
d, err := client.CreateDeviceAndSite(siteAndDevice)
if err != nil {
return nil, nil, err
}

return cfg.startWithInternalErrors(client, d)
}

func NewSenderFromDevice(d *api.Device, errors chan<- error, cfg *Config) (*Sender, error) {
client := cfg.client()
return cfg.start(client, d, errors)
}

// NewSenderFromDeviceWithErrors returns a Sender and an error channel for an existing Device
func NewSenderFromDeviceWithErrors(d *api.Device, cfg *Config) (*Sender, <-chan error, error) {
client := cfg.client()
return cfg.startWithInternalErrors(client, d)
}

func lookupdev(dev *api.Device, err error) (*api.Device, error) {
if err != nil {
switch {
Expand Down
117 changes: 117 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,123 @@ func TestNewSenderWithDeviceNameLeaks(t *testing.T) {
goleak.VerifyNone(t, ignore)
}

func TestNewSenderFromDeviceWithErrors(t *testing.T) {
client, server, device, err := test.NewClientServer()
if err != nil {
t.Fatal(err)
}

flowServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
}))

apiurl = server.URL(test.API)
flowurl = server.URL(flowServer.URL)
metricsurl = server.URL(test.TSDB)

email = client.Email
token = client.Token

config := libkflow.NewConfig(email, token, "test", "0.0.1")
config.OverrideURLs(apiurl, flowurl, metricsurl)

l := stubLeveledLogger{}

registry := metrics.NewRegistry()
metrics2.StartWithSetConf(registry, &l, metricsurl.String(), email, token, "chf")
config.WithRegistry(registry)

s, errors, err := libkflow.NewSenderFromDeviceWithErrors(device, config)

assert.NotNil(t, s)
assert.Nil(t, err)

errorsFromChan := make([]error, 0)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for err := range errors {
errorsFromChan = append(errorsFromChan, err)
}
wg.Done()
}()

s.Send(&flow.Flow{
TimestampNano: time.Now().UnixNano(),
})

s.Stop(time.Second)

wg.Wait()

assert.Len(t, errorsFromChan, 1)
}

func TestNewSenderWithNewSiteAndDeviceWithErrors(t *testing.T) {
client, server, device, err := test.NewClientServer()
if err != nil {
t.Fatal(err)
}

flowServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
}))

apiurl = server.URL(test.API)
flowurl = server.URL(flowServer.URL)
metricsurl = server.URL(test.TSDB)

email = client.Email
token = client.Token

config := libkflow.NewConfig(email, token, "test", "0.0.1")
config.OverrideURLs(apiurl, flowurl, metricsurl)

l := stubLeveledLogger{}

registry := metrics.NewRegistry()
metrics2.StartWithSetConf(registry, &l, metricsurl.String(), email, token, "chf")
config.WithRegistry(registry)

s, errors, err := libkflow.NewSenderWithNewSiteAndDeviceWithErrors(&api.SiteAndDeviceCreate{
Site: &api.SiteCreate{
Title: "",
City: "",
Region: "",
Country: "",
},
Device: &api.DeviceCreate{
AllowNoIP: true,
Name: device.Name,
},
}, config)

assert.NotNil(t, s)
assert.Nil(t, err)

errorsFromChan := make([]error, 0)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for err := range errors {
errorsFromChan = append(errorsFromChan, err)
}
wg.Done()
}()

s.Send(&flow.Flow{
TimestampNano: time.Now().UnixNano(),
})

s.Stop(time.Second)

wg.Wait()

assert.Len(t, errorsFromChan, 1)
}

func TestNewSenderFromDevice(t *testing.T) {
dev, assert := setupLibTest(t)

Expand Down

0 comments on commit b6040d1

Please sign in to comment.