mirror of
https://github.com/ceph/go-ceph
synced 2024-12-24 07:03:00 +00:00
e2a78eec02
The *_command functions in librados and libcephfs document the use of specific free functions for data allocated. These functions are currently just wrappers around C's free() function. However, to be more strictly compliant this change adds a free-function callback to the CommandOutput type and the specific free functions are now used outside the unit tests. Signed-off-by: John Mulligan <jmulligan@redhat.com>
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package cutil
|
|
|
|
import (
|
|
"testing"
|
|
"unsafe"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommandOutput(t *testing.T) {
|
|
t.Run("newAndFree", func(t *testing.T) {
|
|
co := NewCommandOutput()
|
|
assert.NotNil(t, co)
|
|
co.Free()
|
|
})
|
|
t.Run("setValues", func(t *testing.T) {
|
|
co := NewCommandOutput()
|
|
assert.NotNil(t, co)
|
|
defer co.Free()
|
|
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
|
|
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
|
|
b, s := co.GoValues()
|
|
assert.EqualValues(t, []byte("i got style"), b)
|
|
assert.EqualValues(t, "i got rhythm", s)
|
|
})
|
|
t.Run("setOnlyOutBuf", func(t *testing.T) {
|
|
co := NewCommandOutput()
|
|
assert.NotNil(t, co)
|
|
defer co.Free()
|
|
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
|
|
b, s := co.GoValues()
|
|
assert.EqualValues(t, []byte("i got style"), b)
|
|
assert.EqualValues(t, "", s)
|
|
})
|
|
t.Run("setOnlyOuts", func(t *testing.T) {
|
|
co := NewCommandOutput()
|
|
assert.NotNil(t, co)
|
|
defer co.Free()
|
|
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
|
|
b, s := co.GoValues()
|
|
assert.Nil(t, b)
|
|
assert.EqualValues(t, "i got rhythm", s)
|
|
})
|
|
t.Run("customFreeFunc", func(t *testing.T) {
|
|
callCount := 0
|
|
co := NewCommandOutput().SetFreeFunc(func(p unsafe.Pointer) {
|
|
callCount++
|
|
free(p)
|
|
})
|
|
assert.NotNil(t, co)
|
|
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
|
|
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
|
|
co.Free()
|
|
assert.Equal(t, 2, callCount)
|
|
})
|
|
}
|