Skip to content

Commit

Permalink
Merge branch 'arve/upgrade-lint' into arve/upgrade-golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <[email protected]>
  • Loading branch information
aknuds1 committed Jun 17, 2024
2 parents 9f27537 + 43ae462 commit 1002687
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
9 changes: 1 addition & 8 deletions pkg/mimirpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mimirpb
import (
stdlibjson "encoding/json"
"math"
"reflect"
"strconv"
"testing"
"unsafe"
Expand Down Expand Up @@ -197,13 +196,7 @@ func TestFromLabelAdaptersToLabelsWithCopy(t *testing.T) {

// All strings must be copied.
actualValue := actual.Get("hello")
// Ignore deprecation warning for now
//nolint:staticcheck
hInputValue := (*reflect.StringHeader)(unsafe.Pointer(&input[0].Value))
// Ignore deprecation warning for now
//nolint:staticcheck
hActualValue := (*reflect.StringHeader)(unsafe.Pointer(&actualValue))
assert.NotEqual(t, hInputValue.Data, hActualValue.Data)
assert.NotSame(t, unsafe.StringData(input[0].Value), unsafe.StringData(actualValue))
}

func BenchmarkFromLabelAdaptersToLabelsWithCopy(b *testing.B) {
Expand Down
29 changes: 14 additions & 15 deletions pkg/mimirpb/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,35 @@ func TestTimeseriesFromPool(t *testing.T) {
}

func TestCopyToYoloString(t *testing.T) {
stringByteArray := func(val string) uintptr {
// Ignore deprecation warning for now
//nolint:staticcheck
return (*reflect.SliceHeader)(unsafe.Pointer(&val)).Data
}

testString := yoloString([]byte("testString"))
testStringByteArray := stringByteArray(testString)
testStringByteArray := unsafe.StringData(testString)

// Verify that the unsafe copy is unsafe.
unsafeCopy := testString
unsafeCopyByteArray := stringByteArray(unsafeCopy)
assert.Equal(t, testStringByteArray, unsafeCopyByteArray)
unsafeCopyByteArray := unsafe.StringData(unsafeCopy)
assert.Same(t, testStringByteArray, unsafeCopyByteArray)

// Create a safe copy by using the newBuf byte slice.
newBuf := make([]byte, 0, len(testString))
safeCopy, remainingBuf := copyToYoloString(newBuf, unsafeCopy)

// Verify that the safe copy is safe by checking that the underlying byte arrays are different.
safeCopyByteArray := stringByteArray(safeCopy)
assert.NotEqual(t, testStringByteArray, safeCopyByteArray)
safeCopyByteArray := unsafe.StringData(safeCopy)
assert.NotSame(t, testStringByteArray, safeCopyByteArray)

// Verify that the remainingBuf has been used up completely.
assert.Len(t, remainingBuf, 0)

// Verify that the remainingBuf is using the same underlying byte array as safeCopy but advanced by the length.
// Ignore deprecation warning for now
//nolint:staticcheck
remainingBufArray := (*reflect.SliceHeader)(unsafe.Pointer(&remainingBuf)).Data
assert.Equal(t, int(safeCopyByteArray)+len(newBuf), int(remainingBufArray))
remainingBufArrayData := unsafe.SliceData(remainingBuf)
/*
// Ignore deprecation warning for now
//nolint:staticcheck
remainingBufArray := (*reflect.SliceHeader)(unsafe.Pointer(&remainingBuf)).Data
t.Logf("remainingBufArray should be at %d, is at %d", remainingBufArray, int(uintptr(unsafe.Pointer(&safeCopy)))+len(newBuf))
*/
//assert.Equal(t, int(safeCopyByteArray)+len(newBuf), int(remainingBufArray))
assert.Equal(t, uintptr(unsafe.Add(unsafe.Pointer(&safeCopy), len(newBuf))), uintptr(unsafe.Pointer(remainingBufArrayData)))
}

func TestDeepCopyTimeseries(t *testing.T) {
Expand Down

0 comments on commit 1002687

Please sign in to comment.