Skip to content

Commit

Permalink
Merge pull request #253 from ecodeclub/dev
Browse files Browse the repository at this point in the history
添加 v.0.0.9 的例子
  • Loading branch information
flycash authored Apr 16, 2024
2 parents bdab172 + abf484f commit 5e51826
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=cover.out -v ./...
run: go test -race -coverprofile=cover.out ./...

- name: Post Coverage
uses: codecov/codecov-action@v2
36 changes: 36 additions & 0 deletions iox/json_reader_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021 ecodeclub
//
// 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 iox_test

import (
"fmt"
"net/http"

"github.com/ecodeclub/ekit/iox"
)

func ExampleNewJSONReader() {
val := iox.NewJSONReader(User{Name: "Tom"})
_, err := http.NewRequest(http.MethodPost, "/hello", val)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("OK")
}

type User struct {
Name string `json:"name"`
}
31 changes: 31 additions & 0 deletions list/skip_list_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 ecodeclub
//
// 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 list_test

import (
"fmt"

"github.com/ecodeclub/ekit"
"github.com/ecodeclub/ekit/internal/list"
)

func ExampleNewSkipList() {
l := list.NewSkipList[int](ekit.ComparatorRealNumber[int])
l.Insert(123)
val, _ := l.Get(0)
fmt.Println(val)
// Output:
// 123
}
33 changes: 33 additions & 0 deletions queue/priority_queue_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 ecodeclub
//
// 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 queue_test

import (
"fmt"

"github.com/ecodeclub/ekit"
"github.com/ecodeclub/ekit/internal/queue"
)

func ExampleNewPriorityQueue() {
// 容量,并且队列里面放的是 int
pq := queue.NewPriorityQueue(10, ekit.ComparatorRealNumber[int])
_ = pq.Enqueue(10)
_ = pq.Enqueue(9)
val, _ := pq.Dequeue()
fmt.Println(val)
// Output:
// 9
}
8 changes: 5 additions & 3 deletions syncx/limit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ func NewLimitPool[T any](maxTokens int, factory func() T) *LimitPool[T] {
}

// Get 取出一个元素
func (l *LimitPool[T]) Get() T {
// 如果返回值是 true,则代表确实从 Pool 里面取出来了一个
// 否则是新建了一个
func (l *LimitPool[T]) Get() (T, bool) {
if l.tokens.Add(-1) < 0 {
l.tokens.Add(1)
var zero T
return zero
return zero, false
}
return l.pool.Get()
return l.pool.Get(), true
}

// Put 放回去一个元素
Expand Down
38 changes: 38 additions & 0 deletions syncx/limit_pool_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 ecodeclub
//
// 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 syncx_test

import (
"fmt"

"github.com/ecodeclub/ekit/syncx"
)

func ExampleNewLimitPool() {
p := syncx.NewLimitPool(1, func() int {
return 123
})
val, ok := p.Get()
fmt.Println("第一次", val, ok)
val, ok = p.Get()
fmt.Println("第二次", val, ok)
p.Put(123)
val, ok = p.Get()
fmt.Println("第三次", val, ok)
// Output:
// 第一次 123 true
// 第二次 0 false
// 第三次 123 true
}
16 changes: 11 additions & 5 deletions syncx/limit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestLimitPool(t *testing.T) {
go func() {
defer wg.Done()

buf := pool.Get()

buf, ok := pool.Get()
assert.True(t, ok)
assert.NotZero(t, buf)
assert.Equal(t, string(expectedVal), string(buf))

Expand All @@ -55,14 +55,20 @@ func TestLimitPool(t *testing.T) {
close(bufChan)

// 超过最大申请次数返回零值
assert.Zero(t, pool.Get())
val, ok := pool.Get()
assert.False(t, ok)
assert.Zero(t, val)

// 归还一个
pool.Put(<-bufChan)

// 再次申请仍可以拿到非零值缓冲区
assert.NotZero(t, string(expectedVal), string(pool.Get()))
val, ok = pool.Get()
assert.True(t, ok)
assert.NotZero(t, string(expectedVal), string(val))

// 超过最大申请次数返回零值
assert.Zero(t, pool.Get())
val, ok = pool.Get()
assert.False(t, ok)
assert.Zero(t, val)
}
35 changes: 35 additions & 0 deletions syncx/segment_key_lock_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 ecodeclub
//
// 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 syncx_test

import (
"fmt"

"github.com/ecodeclub/ekit/syncx"
)

func ExampleNewSegmentKeysLock() {
// 参数就是分多少段,你也可以理解为总共有多少锁
// 锁越多,并发竞争越低,但是消耗内存;
// 锁越少,并发竞争越高,但是内存消耗少;
lock := syncx.NewSegmentKeysLock(100)
// 对应的还有 TryLock
// RLock 和 RUnlock
lock.Lock("key1")
defer lock.Unlock("key1")
fmt.Println("OK")
// Output:
// OK
}

0 comments on commit 5e51826

Please sign in to comment.