Skip to content

Commit

Permalink
fix&test(counter&max_counter): coverage all test.
Browse files Browse the repository at this point in the history
1. fix release method;
2. add test;
  • Loading branch information
gemone committed May 27, 2023
1 parent dd21f66 commit 7f82540
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
3 changes: 3 additions & 0 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func AcquireCounter() *Counter {

// ReleaseCounter releases a Counter Pointer.
func ReleaseCounter(c *Counter) {
if c == nil {
return
}
c.reset()
counterPool.Put(c)
}
Expand Down
5 changes: 4 additions & 1 deletion counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"time"
)

func TestCounterReleaseNil(t *testing.T) {
ReleaseCounter(nil)
}

func TestNewCounter(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -127,7 +131,6 @@ func testCopyTo(t *testing.T) {

ReleaseCounter(c2)
ReleaseCounter(c1)

}
}

Expand Down
18 changes: 15 additions & 3 deletions max_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ func AcquireMaxCounter(max float64) *MaxCounter {

// ReleaseMaxCounter releases MaxCounter.
func ReleaseMaxCounter(c *MaxCounter) {
c.reset()
if c == nil {
return
}

c.reset()
ReleaseCounter(c.counter)
c.counter = nil
maxCounterPool.Put(c)
Expand All @@ -45,7 +48,11 @@ func ReleaseMaxCounter(c *MaxCounter) {
// reset MaxCounter
// And releases Counter
func (c *MaxCounter) reset() {
c.counter.Reset()
if c.counter != nil {
c.counter.Reset()
} else {
c.counter = AcquireCounter()
}
atomic.StoreUint64(&c.maxBits, 0)
atomic.StoreUint32(&c.done, 0)
}
Expand Down Expand Up @@ -158,7 +165,7 @@ func (c *MaxCounter) Dec() bool {
return c.Add(-1)
}

// CopyTo copies number to dst
// CopyTo copies number to dst.
func (c *MaxCounter) CopyTo(d interface{}) (ok bool, err error) {
dst, can := d.(*MaxCounter)
if !can {
Expand All @@ -167,6 +174,7 @@ func (c *MaxCounter) CopyTo(d interface{}) (ok bool, err error) {
}

if c == dst {
err = ErrSameCounterPointer
return
}

Expand All @@ -177,6 +185,10 @@ func (c *MaxCounter) CopyTo(d interface{}) (ok bool, err error) {
oldCounter := dst.counter
counter := c.counter

if counter == nil {
counter = AcquireCounter()
}

if oldCounter == nil {
oldCounter = AcquireCounter()
}
Expand Down
77 changes: 77 additions & 0 deletions max_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"time"
)

func TestMaxCounterReleaseNil(t *testing.T) {
ReleaseMaxCounter(nil)
}

func TestMaxCounterSetMax(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -97,6 +101,17 @@ func TestMaxCounterAddAndSub(t *testing.T) {
}
}

func TestMaxCounter_CopyTo(t *testing.T) {
t.Parallel()

testMaxCounterCopyTo(t)

testGo(t, testMaxCounterCopyTo, 10)
testGo(t, testMaxCounterCopyTo, 100)
testGo(t, testMaxCounterCopyTo, 1000)
testGo(t, testMaxCounterCopyTo, 10000)
}

func TestMaxCounter_Reset(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -219,3 +234,65 @@ func testMaxCounterCount(t *testing.T) {
t.Fatalf("should be %d, but %f", 0, v)
}
}

func testMaxCounterCopyTo(t *testing.T) {
for i := 0; i < 10; i++ {
c1 := AcquireMaxCounter(50)
c2 := AcquireMaxCounter(50)

// c1 to c1
ok, err := c1.CopyTo(c1)
if ok {
t.Fatal("same counter should err, but not!")
}
if err != ErrSameCounterPointer {
t.Fatalf("same counter should err, but %s", err)
}
// c2 to c2
ok, err = c2.CopyTo(c2)
if ok {
t.Fatal("same counter should err, but not!")
}
if err != ErrSameCounterPointer {
t.Fatalf("same counter should err, but %s", err.Error())
}

// copy to
ok, err = c1.CopyTo(c2)
if !ok {
t.Fatalf("counter should be copied, but not, %v", err)
}

// copy to other
ok, err = c1.CopyTo(1)
if ok || err == nil {
t.Fatal("should err, but not")
}

// counter nil
c1.counter = nil
ok, err = c1.CopyTo(c2)
if !ok {
t.Fatalf("should ok, but not, %v", err)
}

c2.counter = nil
ok, err = c1.CopyTo(c2)
if !ok {
t.Fatal("should ok, but not")
}

// copy same, should err
c1Counter := c1.counter
c1.counter = c2.counter
ok, _ = c1.CopyTo(c2)
if ok {
t.Fatal("should err, but not")
}
// fix error
c1.counter = c1Counter

ReleaseMaxCounter(c2)
ReleaseMaxCounter(c1)
}
}

0 comments on commit 7f82540

Please sign in to comment.