Skip to content
Merged
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
5 changes: 4 additions & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ header:
- '**/*.go'
- '**/*.s'

comment: on-failure
paths-ignore:
- 'internal/httpguts/**'

comment: on-failure
1 change: 0 additions & 1 deletion CREDITS
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
github.com/stretchr/testify
golang.org/x/sync
golang.org/x/sys
12 changes: 6 additions & 6 deletions cache/asynccache/asynccache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/bytedance/gopkg/internal/assert"
)

func TestGetOK(t *testing.T) {
Expand All @@ -36,18 +36,18 @@ func TestGetOK(t *testing.T) {
c := NewAsyncCache(op)

v, err := c.Get(key)
assert.NoError(t, err)
assert.Nil(t, err)
assert.Equal(t, v.(string), ret)

time.Sleep(time.Second / 2)
ret = "change"
v, err = c.Get(key)
assert.NoError(t, err)
assert.Nil(t, err)
assert.NotEqual(t, v.(string), ret)

time.Sleep(time.Second)
v, err = c.Get(key)
assert.NoError(t, err)
assert.Nil(t, err)
assert.Equal(t, v.(string), ret)
}

Expand All @@ -70,7 +70,7 @@ func TestGetErr(t *testing.T) {
c := NewAsyncCache(op)

v, err := c.Get(key)
assert.Error(t, err)
assert.True(t, err != nil)
assert.Nil(t, v)

time.Sleep(time.Second / 2)
Expand All @@ -79,7 +79,7 @@ func TestGetErr(t *testing.T) {

time.Sleep(time.Second + 10*time.Millisecond)
v, err = c.Get(key)
assert.NoError(t, err)
assert.Nil(t, err)
assert.Equal(t, v.(string), ret)
}

Expand Down
15 changes: 0 additions & 15 deletions check_branch_name.sh

This file was deleted.

2 changes: 1 addition & 1 deletion cloud/metainfo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"strings"

"golang.org/x/net/http/httpguts"
"github.com/bytedance/gopkg/internal/httpguts"
)

// HTTP header prefixes.
Expand Down
15 changes: 15 additions & 0 deletions cloud/metainfo/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ func TestToHTTPHeader(t *testing.T) {
assert(t, h.Get(metainfo.HTTPPrefixPersistent+"abc") == "def")
}

func TestToHTTPHeaderDiscardInvalid(t *testing.T) {
h := make(http.Header)
c := context.Background()
c = metainfo.WithValue(c, "valid", "ok")
c = metainfo.WithValue(c, "bad key", "dropped")
c = metainfo.WithPersistentValue(c, "persist", "still-ok")
c = metainfo.WithPersistentValue(c, "bad", "line\nbreak")

metainfo.ToHTTPHeader(c, h)

assert(t, len(h) == 2, h)
assert(t, h.Get(metainfo.HTTPPrefixTransient+"valid") == "ok")
assert(t, h.Get(metainfo.HTTPPrefixPersistent+"persist") == "still-ok")
}

func TestHTTPHeader(t *testing.T) {
h := make(metainfo.HTTPHeader)
h.Set("Hello", "halo")
Expand Down
27 changes: 16 additions & 11 deletions collection/zset/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"testing"
"time"

"github.com/bytedance/gopkg/internal/assert"
"github.com/bytedance/gopkg/lang/fastrand"
"github.com/stretchr/testify/assert"
)

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand All @@ -39,7 +39,7 @@ func randString(prefix string) string {

func TestFloat64Set(t *testing.T) {
z := NewFloat64()
assert.Zero(t, z.Len())
assert.True(t, z.Len() == 0)
}

func TestFloat64SetAdd(t *testing.T) {
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestFloat64SetRank_UpdateScore(t *testing.T) {
for _, v := range vs {
r := z.Rank(v)
assert.NotEqual(t, -1, r)
assert.Greater(t, z.Len(), r)
assert.True(t, z.Len() > r)

// verify rank by traversing level 0
actualRank := 0
Expand Down Expand Up @@ -237,11 +237,16 @@ func testInternalSpan(t *testing.T, z *Float64Set) {
assert.NotEqual(t, -1, toRank)

// span = to.rank - from.rank
assert.Equalf(t, span, toRank-fromRank, "from %q (score: , rank: %d) to %q (score: %d, rank: %d), expect span: %d, actual: %d",
from, fromScore, fromRank, to, toScore, toRank, span, toRank-fromRank)
if span != toRank-fromRank {
t.Fatalf("from %q (score: %v, rank: %d) to %q (score: %v, rank: %d), expect span: %d, actual: %d",
from, fromScore, fromRank, to, toScore, toRank, span, toRank-fromRank)
}
} else { // from -> nil
// span = skiplist.len - from.rank
assert.Equalf(t, l.length-fromRank, x.loadSpan(i), "%q (score: , rank: %d)", from, fromScore, fromRank)
if l.length-fromRank != x.loadSpan(i) {
t.Fatalf("%q (score: %v, rank: %d): expect span %d, actual %d",
from, fromScore, fromRank, l.length-fromRank, x.loadSpan(i))
}
}
}
}
Expand Down Expand Up @@ -408,8 +413,8 @@ func testFloat64SetRangeByScore(t *testing.T, rev bool) {
}
var prev *float64
for _, n := range ns {
assert.LessOrEqual(t, min, n.Score)
assert.GreaterOrEqual(t, max, n.Score)
assert.True(t, min <= n.Score)
assert.True(t, max >= n.Score)
if prev != nil {
if rev {
assert.True(t, *prev >= n.Score)
Expand Down Expand Up @@ -598,7 +603,7 @@ func TestUnionFloat64(t *testing.T) {

func TestUnionFloat64_Empty(t *testing.T) {
z := UnionFloat64()
assert.Zero(t, z.Len())
assert.True(t, z.Len() == 0)
}

func TestInterFloat64(t *testing.T) {
Expand Down Expand Up @@ -626,7 +631,7 @@ func TestInterFloat64(t *testing.T) {

func TestInterFloat64_Empty(t *testing.T) {
z := InterFloat64()
assert.Zero(t, z.Len())
assert.True(t, z.Len() == 0)
}

func TestInterFloat64_Simple(t *testing.T) {
Expand All @@ -638,7 +643,7 @@ func TestInterFloat64_Simple(t *testing.T) {
z3.Add(0, "2")

z := InterFloat64(z1, z2, z3)
assert.Zero(t, z.Len())
assert.True(t, z.Len() == 0)
}

func BenchmarkRankAfterDelete(b *testing.B) {
Expand Down
13 changes: 2 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ module github.com/bytedance/gopkg
go 1.18

require (
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.24.0
golang.org/x/sync v0.8.0
golang.org/x/sys v0.19.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
golang.org/x/sync v0.11.0
golang.org/x/sys v0.30.0
)
22 changes: 4 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
69 changes: 69 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2025 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package assert

import (
"reflect"
"testing"
)

func Equal(t *testing.T, expected, actual any) {
t.Helper()
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected %#v, got %#v", expected, actual)
}
}

func NotEqual(t *testing.T, expected, actual any) {
t.Helper()
if reflect.DeepEqual(expected, actual) {
t.Fatalf("did not expect %#v", actual)
}
}

func True(t *testing.T, cond bool) {
t.Helper()
if !cond {
t.Fatal("expected true")
}
}

func False(t *testing.T, cond bool) {
t.Helper()
if cond {
t.Fatal("expected false")
}
}

func Nil(t *testing.T, v any) {
t.Helper()
if isNil(v) {
return
}
t.Fatalf("expected nil, got %#v", v)
}

func isNil(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
49 changes: 49 additions & 0 deletions internal/assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2025 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package assert

import "testing"

type nilTestStruct struct{}

func TestEqual(t *testing.T) {
Equal(t, []int{1, 2}, []int{1, 2})
}

func TestNotEqual(t *testing.T) {
NotEqual(t, []int{1, 2}, []int{2, 1})
}

func TestTrue(t *testing.T) {
True(t, true)
}

func TestFalse(t *testing.T) {
False(t, false)
}

func TestNil(t *testing.T) {
var p *nilTestStruct
Nil(t, p)

var m map[string]int
Nil(t, m)

var s []int
Nil(t, s)

var i interface{} = p
Nil(t, i)
}
24 changes: 24 additions & 0 deletions internal/httpguts/LICENSE.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright 2009 The Go Authors.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions internal/httpguts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Derived from golang.org/x/net/http/httpguts
// Source: https://go.googlesource.com/net/+/refs/tags/v0.52.0/http/httpguts/
Loading
Loading