Sfoglia il codice sorgente

Accommodate zero Count(); fix Reset() bug

Jonathan D. Storm 3 settimane fa
parent
commit
721758c7ab
2 ha cambiato i file con 10 aggiunte e 4 eliminazioni
  1. 9 3
      v6/depager.go
  2. 1 1
      v6/depager_test.go

+ 9 - 3
v6/depager.go

@@ -107,7 +107,7 @@ func (p *pager[T]) iteratePages() <-chan Page[T] {
 		defer close(ch)
 		var page Page[T]
 		for {
-			if p.ctx.Err() != nil {
+			if p.pctx.Err() != nil {
 				break
 			}
 			page = <-p.pagePool
@@ -120,6 +120,12 @@ func (p *pager[T]) iteratePages() <-chan Page[T] {
 			if p.cnt == 0 {
 				p.cnt = page.Count()
 			}
+			// When page.Count() is zero, we must rely on the
+			// absence of returned results to know when to stop.
+			if p.cnt == 0 && len(page.Elems()) == 0 {
+				p.pagePool <- page
+				return
+			}
 			ch <- page
 
 			if (p.m*p.n + p.n) >= p.cnt {
@@ -137,7 +143,7 @@ func (p *pager[T]) IterPages() <-chan Page[T] {
 		defer close(p.done)
 		defer close(ch)
 		for page := range p.iteratePages() {
-			if p.ctx.Err() != nil {
+			if p.pctx.Err() != nil {
 				p.pagePool <- page
 				break
 			}
@@ -158,7 +164,7 @@ func (p *pager[T]) Iter() <-chan T {
 		defer close(p.done)
 		defer close(ch)
 		for page := range p.iteratePages() {
-			if p.ctx.Err() != nil {
+			if p.pctx.Err() != nil {
 				p.pagePool <- page
 				break
 			}

+ 1 - 1
v6/depager_test.go

@@ -234,7 +234,7 @@ func performAbortTest(t *testing.T, poolLen, poolCap int) {
 	}
 
 	// Return pages to pool
-	for i := 0; i < len(ch); i++ {
+	for len(ch) > 0 {
 		pagePool <- <-ch
 	}