Skip to content

Commit

Permalink
feat(container/garray): remove unnecessary RLock for MarshalJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
heas committed Jan 22, 2025
1 parent 99f0fb1 commit 89ff94c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 0 additions & 2 deletions container/garray/garray_normal_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,6 @@ func (a *Array) String() string {
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
// Note that do not use pointer as its receiver here.
func (a Array) MarshalJSON() ([]byte, error) {
a.mu.RLock()
defer a.mu.RUnlock()
return json.Marshal(a.array)
}

Expand Down
22 changes: 22 additions & 0 deletions container/garray/garray_z_unit_normal_any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,25 @@ func TestArray_Walk(t *testing.T) {
}), g.Slice{"key-1", "key-2"})
})
}

func TestArray_MarshalJSON(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewArrayFrom([]interface{}{1, 2, 3}, true)

go func() {
time.Sleep(1 * time.Second)
array.Append(4)
}()

// MarshalJSON code here, just replace:
//func (a Array) MarshalJSON() ([]byte, error) {
// fmt.Println(fmt.Sprintf("before=%+v", a.array))
// time.Sleep(2 * time.Second)
// fmt.Println(fmt.Sprintf("after=%+v", a.array))
// return json.Marshal(a.array)
//}
data, err := array.MarshalJSON()
t.AssertNil(err)
t.Assert(data, []byte(`[1,2,3]`))
})
}

0 comments on commit 89ff94c

Please sign in to comment.