Simplify one shot tests (#1786)

This commit is contained in:
Shivam Sandbhor 2022-10-06 15:27:26 +05:30 committed by GitHub
parent 3ba67bad3d
commit 65c0b9ebcf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 65 deletions

View file

@ -757,48 +757,25 @@ func TestOneShotAcquisition(t *testing.T) {
if tc.setup != nil { if tc.setup != nil {
tc.setup(t, &cw) tc.setup(t, &cw)
} }
out := make(chan types.Event) out := make(chan types.Event, 100)
tmb := tomb.Tomb{} tmb := tomb.Tomb{}
var rcvdEvts []types.Event var rcvdEvts []types.Event
dbgLogger.Infof("running StreamingAcquisition") dbgLogger.Infof("running StreamingAcquisition")
actmb := tomb.Tomb{} err = cw.OneShotAcquisition(out, &tmb)
actmb.Go(func() error { dbgLogger.Infof("acquis done")
err := cw.OneShotAcquisition(out, &actmb) cstest.RequireErrorContains(t, err, tc.expectedStartErr)
dbgLogger.Infof("acquis done") close(out)
cstest.RequireErrorContains(t, err, tc.expectedStartErr)
return nil
})
// let's empty output chan // let's empty output chan
tmb.Go(func() error { for evt := range out {
for { rcvdEvts = append(rcvdEvts, evt)
select { }
case in := <-out:
log.Debugf("received event %+v", in)
rcvdEvts = append(rcvdEvts, in)
case <-tmb.Dying():
log.Debugf("pumper died")
return nil
}
}
})
if tc.run != nil { if tc.run != nil {
tc.run(t, &cw) tc.run(t, &cw)
} else { } else {
dbgLogger.Warning("no code to run") dbgLogger.Warning("no code to run")
} }
time.Sleep(5 * time.Second)
dbgLogger.Infof("killing collector")
tmb.Kill(nil)
<-tmb.Dead()
dbgLogger.Infof("killing datasource")
actmb.Kill(nil)
dbgLogger.Infof("waiting datasource death")
<-actmb.Dead()
// check results
if tc.expectedResLen != -1 { if tc.expectedResLen != -1 {
if tc.expectedResLen != len(rcvdEvts) { if tc.expectedResLen != len(rcvdEvts) {
t.Fatalf("%s : expected %d results got %d -> %v", tc.name, tc.expectedResLen, len(rcvdEvts), rcvdEvts) t.Fatalf("%s : expected %d results got %d -> %v", tc.name, tc.expectedResLen, len(rcvdEvts), rcvdEvts)

View file

@ -307,29 +307,14 @@ func TestOneShot(t *testing.T) {
t.Fatalf("unable to configure dsn '%s': %s", ts.dsn, err) t.Fatalf("unable to configure dsn '%s': %s", ts.dsn, err)
} }
dockerClient.Client = new(mockDockerCli) dockerClient.Client = new(mockDockerCli)
out := make(chan types.Event) out := make(chan types.Event, 100)
actualLines := 0
if ts.expectedLines != 0 {
go func() {
READLOOP:
for {
select {
case <-out:
actualLines++
case <-time.After(1 * time.Second):
break READLOOP
}
}
}()
}
tomb := tomb.Tomb{} tomb := tomb.Tomb{}
err := dockerClient.OneShotAcquisition(out, &tomb) err := dockerClient.OneShotAcquisition(out, &tomb)
cstest.AssertErrorContains(t, err, ts.expectedErr) cstest.AssertErrorContains(t, err, ts.expectedErr)
// else we do the check before actualLines is incremented ... // else we do the check before actualLines is incremented ...
time.Sleep(1 * time.Second)
if ts.expectedLines != 0 { if ts.expectedLines != 0 {
assert.Equal(t, ts.expectedLines, actualLines) assert.Equal(t, ts.expectedLines, len(out))
} }
} }

View file

@ -151,27 +151,12 @@ journalctl_filter:
}) })
} }
tomb := tomb.Tomb{} tomb := tomb.Tomb{}
out := make(chan types.Event) out := make(chan types.Event, 100)
j := JournalCtlSource{} j := JournalCtlSource{}
err := j.Configure([]byte(ts.config), subLogger) err := j.Configure([]byte(ts.config), subLogger)
if err != nil { if err != nil {
t.Fatalf("Unexpected error : %s", err) t.Fatalf("Unexpected error : %s", err)
} }
actualLines := 0
if ts.expectedLines != 0 {
go func() {
READLOOP:
for {
select {
case <-out:
actualLines++
case <-time.After(1 * time.Second):
break READLOOP
}
}
}()
}
err = j.OneShotAcquisition(out, &tomb) err = j.OneShotAcquisition(out, &tomb)
cstest.AssertErrorContains(t, err, ts.expectedErr) cstest.AssertErrorContains(t, err, ts.expectedErr)
if err != nil { if err != nil {
@ -179,7 +164,7 @@ journalctl_filter:
} }
if ts.expectedLines != 0 { if ts.expectedLines != 0 {
assert.Equal(t, ts.expectedLines, actualLines) assert.Equal(t, ts.expectedLines, len(out))
} }
if ts.expectedOutput != "" { if ts.expectedOutput != "" {