func (c *Consumer) write( n store.NamedIterator, hostName, appName string) error { var r store.Record c.toBeCommitted = append(c.toBeCommitted, n) for n.Next(&r) { c.buffer[c.idx] = Record{ HostName: hostName, Tags: TagGroup{TagAppName: appName}, Path: r.Info.Path(), Kind: r.Info.Kind(), SubType: r.Info.SubType(), Unit: r.Info.Unit(), Value: r.Value, Timestamp: duration.FloatToTime(r.TimeStamp), } c.idx++ if c.idx == len(c.buffer) { err := c.flush() if err != nil { return err } c.toBeCommitted = append(c.toBeCommitted, n) } } return nil }
func TestCoordinate(t *testing.T) { var iter store.NamedIterator = &fakeTsIterator{ Current: 200.0, End: 500.0, Incr: 10.0, } coord := &fakeCoordinator{} var skipped uint64 updateSkipped := func(x uint64) { skipped += x } // Leases go up in 30 second increments iter = store.NamedIteratorCoordinate(iter, coord, 30.0, updateSkipped) runIteration(t, iter, 200.0, 210.0, 220.0) // Verify we are on 1st lease and it goes from 0 to 230 coord.Verify(t, 1, 0.0, 230.0) runIteration(t, iter, 230.0, 240.0, 250.0, 260.0) // Verify we are on 3rd lease and it goes from 0 to 290 coord.Verify(t, 3, 0.0, 290.0) iter.Commit() if skipped != 0 { t.Errorf("Expected 0 skipped, got %d", skipped) } coord.SetNextStart(340.0) // Even though we are on new start, current lease is still valid. runIteration(t, iter, 270.0, 280.0) coord.Verify(t, 3, 0.0, 290.0) // Now we jump to 340 runIteration(t, iter, 340.0) // skip isn't updated until after we commit if skipped != 0 { t.Errorf("Expected 0 skipped, got %d", skipped) } iter.Commit() if skipped != 5 { t.Errorf("Expected 5 skipped, got %d", skipped) } // 4th lease goes from 340 to 370 coord.Verify(t, 4, 340.0, 370.0) runIteration(t, iter, 350.0, 360.0, 370.0) // 5th lease runs to 400 coord.Verify(t, 5, 340.0, 400.0) coord.SetNextStart(600.0) runIteration(t, iter, 380.0, 390) coord.Verify(t, 5, 340.0, 400.0) var rec store.Record if iter.Next(&rec) { t.Error("Exected no more, but got some") } coord.Verify(t, 6, 600.0, 630.0) // Skip not updated until after we commit if skipped != 5 { t.Errorf("Expected 5 skipped, got %d", skipped) } iter.Commit() // Our iterator goes to 500 so we skipped last 10 making total skipped // be 15. if skipped != 15 { t.Errorf("Expected 15 skipped, got %d", skipped) } }