-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat[pool]: Add new interfaces for compatibility
- Loading branch information
1 parent
7b5cdbe
commit 29cd4ae
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pool | ||
|
||
type Pool[T any] interface { | ||
Get() T | ||
Put(T) | ||
} | ||
|
||
type WithPutError[T any] interface { | ||
Get() T | ||
Put(T) error | ||
} | ||
|
||
type BufferFactoryInterfaceCompat struct { | ||
BufferFactory | ||
} | ||
|
||
func (b BufferFactoryInterfaceCompat) Put(buf *Buffer) { | ||
_ = b.BufferFactory.Put(buf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package pool | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
// ensure compatibility with interface | ||
func TestInterfaces(t *testing.T) { | ||
t.Parallel() | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Error("interface not implemented") | ||
} | ||
}() | ||
var ( | ||
bf any = NewBufferFactory() | ||
bfCompat any = BufferFactoryInterfaceCompat{NewBufferFactory()} | ||
sPool any = &sync.Pool{ | ||
New: func() any { return new(bytes.Buffer) }, | ||
} | ||
) | ||
if _, ok := sPool.(Pool[any]); !ok { | ||
t.Fatal("Pool[any] not implemented by sync.Pool") | ||
} | ||
testMe1, ok1 := bfCompat.(Pool[*Buffer]) | ||
if !ok1 { | ||
t.Fatal("Pool[*Buffer] not implemented") | ||
} | ||
|
||
t.Run("Pool", func(t *testing.T) { | ||
t.Parallel() | ||
b := testMe1.Get() | ||
if _, err := b.WriteString("test"); err != nil { | ||
t.Fatal(err) | ||
} | ||
testMe1.Put(b) | ||
b = testMe1.Get() | ||
if b.Len() != 0 { | ||
t.Fatal("buffer not reset") | ||
} | ||
testMe1.Put(b) | ||
}) | ||
|
||
t.Run("PoolWithPutError", func(t *testing.T) { | ||
t.Parallel() | ||
testMe2, ok2 := bf.(WithPutError[*Buffer]) | ||
if !ok2 { | ||
t.Error("PoolWithPutError[*Buffer] not implemented") | ||
} | ||
b := testMe2.Get() | ||
if _, err := b.WriteString("test"); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := testMe2.Put(b); err != nil { | ||
t.Fatal(err) | ||
} | ||
b = testMe2.Get() | ||
if b.Len() != 0 { | ||
t.Fatal("buffer not reset") | ||
} | ||
if err := testMe2.Put(b); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
} |