Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new 'WithErrors' constructors #28

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if the other public constructors also got this documentation

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of my pet peeves is that assert does not stop the test. So if s is nil here the test will fail but will continue to run. And then we'll see a nil pointer access down below when we call s.Send() and that just makes it harder to figure out what's actually broken from the noisier test output.
It's better to use require for assertions where you would want to fail the test and stop immediately.
Eg.: require.NotNil(t, s)

You don't have to change it if you don't want to. Just trying to get the word out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny you mention this. I first learned about require while reading a PR comment of yours from way back and started using it then. I like it a lot. Think I just got a bit lazy here.

I'll sharpen these tests up a bit.

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
Loading