Skip to content

Commit

Permalink
Add a new state after uuidwait and before dircreation (#32)
Browse files Browse the repository at this point in the history
* Add a new state after uuidwait and before dircreation

* Update state names for reading candidate or saved packets
  • Loading branch information
stephen-soltesz authored Dec 3, 2019
1 parent 419b929 commit c778536
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion saver/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (t *TCP) savePackets(ctx context.Context, uuidDelay, duration time.Duration
// Write PCAP data to the buffer.
w := pcapgo.NewWriterNanos(zip)
// Now save packets until the stream is done or the context is canceled.
t.state.Set("readingpackets")
t.state.Set("readingcandidatepackets")
// Read the first packet to determine the TCP+IP header size (as IPv6 is variable in size)
p, ok := t.readPacket(derivedCtx)
if !ok {
Expand Down Expand Up @@ -223,6 +223,7 @@ func (t *TCP) savePackets(ctx context.Context, uuidDelay, duration time.Duration
}
// uuidEvent is now set to a good value.

t.state.Set("readingsavedpackets")
// Continue reading packets until duration has elapsed.
for {
p, ok := t.readPacket(derivedCtx)
Expand Down
16 changes: 8 additions & 8 deletions saver/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func TestSaverDryRun(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

// Wait until the status is readingpackets or the surrounding context has been cancelled.
// Wait until the status is readingcandidatepackets or the surrounding context has been cancelled.
go func() {
for s.state.Get() != "readingpackets" && ctx.Err() == nil {
for s.state.Get() != "readingcandidatepackets" && ctx.Err() == nil {
time.Sleep(1 * time.Millisecond)
}
cancel()
Expand All @@ -106,7 +106,7 @@ func TestSaverDryRun(t *testing.T) {
s.start(ctx, 5*time.Second, 10*time.Second) // Give the disk IO 10 seconds to happen.
expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "nopacketserror", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "nopacketserror", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestSaverWithUUID(t *testing.T) {

expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "uuidwait", "dircreation", "savingfile", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "uuidwait", "readingsavedpackets", "dircreation", "savingfile", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestSaverNoUUID(t *testing.T) {
s.start(ctx, 5*time.Second, 10*time.Second)
expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "uuidwait", "uuiderror", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "uuidwait", "uuiderror", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestSaverNoUUIDClosedUUIDChan(t *testing.T) {
s.start(context.Background(), 5*time.Second, 10*time.Second)
expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "uuidwait", "uuidchanerror", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "uuidwait", "uuidchanerror", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestSaverCantMkdir(t *testing.T) {

expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "uuidwait", "dircreation", "mkdirerror", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "uuidwait", "readingsavedpackets", "dircreation", "mkdirerror", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestSaverCantCreate(t *testing.T) {

expected := statusTracker{
status: "stopped",
past: []string{"notstarted", "readingpackets", "uuidwait", "dircreation", "savingfile", "filewriteerror", "discardingpackets"},
past: []string{"notstarted", "readingcandidatepackets", "uuidwait", "readingsavedpackets", "dircreation", "savingfile", "filewriteerror", "discardingpackets"},
}
if !reflect.DeepEqual(&tracker, &expected) {
t.Errorf("%+v != %+v", &tracker, &expected)
Expand Down

0 comments on commit c778536

Please sign in to comment.