Skip to content

Commit

Permalink
test(exp/sync): add example test case for NewLRU and NewFixedPool.
Browse files Browse the repository at this point in the history
  • Loading branch information
searKing committed Nov 6, 2024
1 parent 862f8bf commit 367a7a1
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
76 changes: 76 additions & 0 deletions go/exp/sync/example_fixedpool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2024 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sync_test

import (
"bytes"
"fmt"
"time"

sync_ "github.com/searKing/golang/go/exp/sync"
)

var bufFixedPool = sync_.NewFixedPool[*bytes.Buffer](func() *bytes.Buffer {
// The Pool's New function should generally only return pointer
// types, since a pointer can be put into the return interface
// value without an allocation:
fmt.Println("allocating new buffer")
return new(bytes.Buffer)
}, 1)

var bufCachedPool = sync_.NewCachedPool[*bytes.Buffer](func() *bytes.Buffer {
// The Pool's New function should generally only return pointer
// types, since a pointer can be put into the return interface
// value without an allocation:
fmt.Println("allocating new buffer")
return new(bytes.Buffer)
})
var bufTempPool = sync_.NewTempPool[*bytes.Buffer](nil)

// timeNow is a fake version of time.Now for tests.
func timeNow() time.Time {
return time.Unix(1136214245, 0)
}

func Log(bufPool *sync_.FixedPool[*bytes.Buffer], key, val string) {
be := bufPool.Get()
{
b := be.Value
b.Reset()
// Replace this with time.Now() in a real logger.
b.WriteString(timeNow().UTC().Format(time.RFC3339))
b.WriteByte(' ')
b.WriteString(key)
b.WriteByte('=')
b.WriteString(val)
fmt.Println(b.String())
}
bufPool.Put(be)
}
func ExampleNewFixedPool() {
Log(bufFixedPool, "path", "/search?q=flowers")
Log(bufFixedPool, "path", "/search?q=vegetables")
// Output:
// 2006-01-02T15:04:05Z path=/search?q=flowers
// 2006-01-02T15:04:05Z path=/search?q=vegetables
}

func ExampleNewCachedPool() {
Log(bufCachedPool, "path", "/search?q=flowers")
Log(bufCachedPool, "path", "/search?q=vegetables")
// Output:
// allocating new buffer
// 2006-01-02T15:04:05Z path=/search?q=flowers
// 2006-01-02T15:04:05Z path=/search?q=vegetables
}

func ExampleNewTempPool() {
bufTempPool.Emplace(new(bytes.Buffer))
Log(bufTempPool, "path", "/search?q=flowers")
Log(bufTempPool, "path", "/search?q=vegetables")
// Output:
// 2006-01-02T15:04:05Z path=/search?q=flowers
// 2006-01-02T15:04:05Z path=/search?q=vegetables
}
60 changes: 60 additions & 0 deletions go/exp/sync/example_lru_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sync_test

import (
"fmt"

sync_ "github.com/searKing/golang/go/exp/sync"
)

func ExampleNewLRU() {
l := sync_.NewLRU[int, int](2)
var evictCounter int
l.SetEvictCallback(func(k int, v int) {
evictCounter++
fmt.Printf("{%d,%d} evicted as oldest: %d\n", k, v, evictCounter)
})
printAll := func() {
fmt.Printf("lru: ")
first := true
for k, v := range l.All() {
if !first {
fmt.Printf(", ")
}
first = false
fmt.Printf("{%d,%d}", k, v)
}
fmt.Println()
}
l.Add(1, 1)
l.Add(2, 2)
l.Add(3, 3) // evict 1, that's the oldest
printAll()

fmt.Println("try to refresh {2,22}")
l.Add(2, 22) // refresh 2; Now 3 is the oldest
printAll()

fmt.Println("try to remove oldest")
l.RemoveOldest() // evict 2, that's the oldest
printAll()

fmt.Println("try to purge all elements")
l.Purge()
printAll()

// Output:
// {1,1} evicted as oldest: 1
// lru: {2,2}, {3,3}
// try to refresh {2,22}
// lru: {3,3}, {2,22}
// try to remove oldest
// {3,3} evicted as oldest: 2
// lru: {2,22}
// try to purge all elements
// {2,22} evicted as oldest: 3
// lru:
}

0 comments on commit 367a7a1

Please sign in to comment.