Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions service/sharddistributor/store/etcd/etcdtypes/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ func TestAssignedState_JSONMarshalling(t *testing.T) {
}
}

func TestAssignedState_JSONUnmarshalWithZeroLastUpdated(t *testing.T) {
tests := map[string]struct {
jsonStr string
}{
"last_updated is string zero": {
jsonStr: `{"assigned_shards":{"1":{"status":"AssignmentStatusREADY"}},"last_updated":"0"}`,
},
"last_updated is numeric zero": {
jsonStr: `{"assigned_shards":{"1":{"status":"AssignmentStatusREADY"}},"last_updated":0}`,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var unmarshalled AssignedState
err := json.Unmarshal([]byte(tc.jsonStr), &unmarshalled)
require.NoError(t, err)
require.Equal(t, types.AssignmentStatusREADY, unmarshalled.AssignedShards["1"].Status)
require.True(t, time.Time(unmarshalled.LastUpdated).IsZero())
})
}
}

func TestShardStatistics_FieldNumberMatched(t *testing.T) {
require.Equal(t,
reflect.TypeOf(ShardStatistics{}).NumField(),
Expand Down
6 changes: 6 additions & 0 deletions service/sharddistributor/store/etcd/etcdtypes/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ func (t Time) MarshalJSON() ([]byte, error) {

// UnmarshalJSON implements the json.Unmarshaler interface.
// It decodes the time from time.RFC3339Nano format.
// It also handles the special case where the value is "0" or 0 (zero),
// which is treated as the zero value of time.Time.
func (t *Time) UnmarshalJSON(data []byte) error {
str := string(data)
if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
if str == "0" {
*t = Time(time.Time{})
return nil
}
parsed, err := ParseTime(str)
if err != nil {
return err
Expand Down
10 changes: 10 additions & 0 deletions service/sharddistributor/store/etcd/etcdtypes/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func TestTimeUnmarshalJSON(t *testing.T) {
expectTime: Time(time.Date(2025, 11, 17, 23, 2, 3, 456789012, time.UTC)),
expectErr: "",
},
"zero string": {
input: []byte(`"0"`),
expectTime: Time(time.Time{}),
expectErr: "",
},
"zero number": {
input: []byte(`0`),
expectTime: Time(time.Time{}),
expectErr: "",
},
"invalid format": {
input: []byte(`"not-a-time"`),
expectTime: Time{},
Expand Down
Loading