Properly close channel when monitor exits.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2019-10-25 14:11:39 -07:00
parent 705cb01e0c
commit be7cc78aa0
9 changed files with 37 additions and 14 deletions
+6 -2
View File
@@ -63,11 +63,15 @@ func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter
}, nil
}
func (e *logCounter) Count() (count int) {
func (e *logCounter) Count() (count int, err error) {
start := e.clock.Now()
for {
select {
case log := <-e.logCh:
case log, ok := <-e.logCh:
if !ok {
err = fmt.Errorf("log channel closed unexpectedly")
return
}
// We only want to count events up until the time at which we started.
// Otherwise we would run forever
if start.Before(log.Timestamp) {
+4 -1
View File
@@ -120,7 +120,10 @@ func TestCount(t *testing.T) {
fakeClock.Step(2 * timeout)
}
}(tc.logs, logCh)
actualCount := counter.Count()
actualCount, err := counter.Count()
if err != nil {
t.Errorf("unexpected error %v", err)
}
if actualCount != tc.expectedCount {
t.Errorf("got %d; expected %d", actualCount, tc.expectedCount)
}
+1 -1
View File
@@ -17,5 +17,5 @@ limitations under the License.
package types
type LogCounter interface {
Count() int
Count() (int, error)
}