mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-19 04:06:24 +00:00
Properly close channel when monitor exits.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@ limitations under the License.
|
||||
package types
|
||||
|
||||
type LogCounter interface {
|
||||
Count() int
|
||||
Count() (int, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user