From 3c002be3456c4ef691495f272a137e86c71dd496 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 16 Jan 2024 20:57:18 +0530 Subject: [PATCH 1/3] tests: add tests for /go/protoutil/duration Signed-off-by: Manik Rana --- go/protoutil/duration_test.go | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/go/protoutil/duration_test.go b/go/protoutil/duration_test.go index 20f01482563..a507630db07 100644 --- a/go/protoutil/duration_test.go +++ b/go/protoutil/duration_test.go @@ -59,6 +59,16 @@ func TestDurationFromProto(t *testing.T) { isOk: true, shouldErr: true, }, + { + name: "nanoseconds", + in: &vttime.Duration{ + Seconds: 1, + Nanos: 500000000, + }, + expected: time.Second + 500*time.Millisecond, + isOk: true, + shouldErr: false, + }, } for _, tt := range tests { @@ -80,3 +90,40 @@ func TestDurationFromProto(t *testing.T) { }) } } + +func TestDurationToProto(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + in time.Duration + expected *vttime.Duration + }{ + { + name: "success", + in: time.Second * 1000, + expected: &vttime.Duration{Seconds: 1000}, + }, + { + name: "zero duration", + in: 0, + expected: &vttime.Duration{}, + }, + { + name: "nanoseconds", + in: time.Second + 500*time.Millisecond, + expected: &vttime.Duration{Seconds: 1, Nanos: 500000000}, + }, + } + + for _, tt := range tests { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + actual := DurationToProto(tt.in) + assert.Equal(t, tt.expected, actual) + }) + } +} From b5bc3a1e024fd460bcdaa580100199df2f000af8 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 08:33:30 +0530 Subject: [PATCH 2/3] tests: get 100% coverage on protoutil Signed-off-by: Manik Rana --- go/protoutil/duration_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/go/protoutil/duration_test.go b/go/protoutil/duration_test.go index a507630db07..46e5d5f2ce7 100644 --- a/go/protoutil/duration_test.go +++ b/go/protoutil/duration_test.go @@ -69,6 +69,16 @@ func TestDurationFromProto(t *testing.T) { isOk: true, shouldErr: false, }, + { + name: "out of range nanoseconds", + in: &vttime.Duration{ + Seconds: -1, + Nanos: 500000000, + }, + expected: 0, + isOk: true, + shouldErr: true, + }, } for _, tt := range tests { From 803a252b716e5210aaa020cddf6ca7926a8e26bc Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 08:35:23 +0530 Subject: [PATCH 3/3] refactor: remove use of t.parallel() Signed-off-by: Manik Rana --- go/protoutil/duration_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go/protoutil/duration_test.go b/go/protoutil/duration_test.go index 46e5d5f2ce7..468d1ad5f34 100644 --- a/go/protoutil/duration_test.go +++ b/go/protoutil/duration_test.go @@ -26,7 +26,6 @@ import ( ) func TestDurationFromProto(t *testing.T) { - t.Parallel() tests := []struct { name string @@ -85,7 +84,6 @@ func TestDurationFromProto(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { - t.Parallel() actual, ok, err := DurationFromProto(tt.in) if tt.shouldErr { @@ -102,7 +100,6 @@ func TestDurationFromProto(t *testing.T) { } func TestDurationToProto(t *testing.T) { - t.Parallel() tests := []struct { name string @@ -130,7 +127,6 @@ func TestDurationToProto(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { - t.Parallel() actual := DurationToProto(tt.in) assert.Equal(t, tt.expected, actual)