Skip to content

Commit

Permalink
test: add test for the timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Dec 25, 2023
1 parent 18ead1e commit 684cf3c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/pcic/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (c *Chunk) TimeStamp() time.Time {
return time.Unix(int64(c.timestampSec), int64(c.timestampNSec))
}

func (c *Chunk) SetTimestamp(value time.Time) {
c.timestampSec = uint32(value.Unix())
seconds := time.Unix(int64(c.timestampSec), 0)
c.timestampNSec = uint32(value.UnixNano() - seconds.UnixNano())
fmt.Printf("Seconds: %d Nanoseconds: %d\n", c.timestampSec, c.timestampNSec)
}

// Bytes return the data the current Chunk is holding
func (c *Chunk) Bytes() []byte {
return c.data
Expand Down Expand Up @@ -191,6 +198,14 @@ func (c *Chunk) MarshalBinary() (data []byte, err error) {
blob[offsetOfStatusCode:offsetOfTimeStampSec],
c.statusCode,
)
binary.LittleEndian.PutUint32(
blob[offsetOfTimeStampSec:offsetOfTimeStampNsec],
c.timestampSec,
)
binary.LittleEndian.PutUint32(
blob[offsetOfTimeStampNsec:offsetOfData],
c.timestampNSec,
)

return blob, nil
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/pcic/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,31 @@ func TestFrameStatus(t *testing.T) {
"The status of the original and the clone do not match",
)
}

func TestChunkTimeStamp(t *testing.T) {
now := time.Now()
chunk := pcic.NewChunk()
chunk.SetTimestamp(now)
assert.Equal(
t,
now.UnixNano(),
chunk.TimeStamp().UnixNano(),
"No error expected when setting the time stamp",
)
clone := pcic.NewChunk()
data, err := chunk.MarshalBinary()
assert.NoError(t,
err,
"No error expected when creating a binary clone",
)
assert.NoError(t,
clone.UnmarshalBinary(data),
"No error expected when creating a clone from the bytes",
)
assert.Equal(
t,
chunk.TimeStamp(),
clone.TimeStamp(),
"The status of the original and the clone do not match",
)
}

0 comments on commit 684cf3c

Please sign in to comment.