mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
swarm/storage: fix T.Fatal inside a goroutine
This commit is contained in:
parent
fe03b76ffe
commit
0cde6cd4e5
1 changed files with 83 additions and 39 deletions
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -114,19 +116,24 @@ func TestNetStoreGetAndPut(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := make(chan struct{}) // this channel ensures that the gouroutine with the Put does not run earlier than the Get
|
c := make(chan struct{}) // this channel ensures that the gouroutine with the Put does not run earlier than the Get
|
||||||
|
putErrC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
<-c // wait for the Get to be called
|
<-c // wait for the Get to be called
|
||||||
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
||||||
|
|
||||||
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
||||||
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
||||||
t.Fatal("Expected netStore to use a fetcher for the Get call")
|
putErrC <- errors.New("Expected netStore to use a fetcher for the Get call")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := netStore.Put(ctx, chunk)
|
err := netStore.Put(ctx, chunk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no err got %v", err)
|
putErrC <- fmt.Errorf("Expected no err got %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putErrC <- nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
close(c)
|
close(c)
|
||||||
|
|
@ -134,6 +141,10 @@ func TestNetStoreGetAndPut(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no err got %v", err)
|
t.Fatalf("Expected no err got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := <-putErrC; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
// the retrieved chunk should be the same as what we Put
|
// the retrieved chunk should be the same as what we Put
|
||||||
if !bytes.Equal(recChunk.Address(), chunk.Address()) || !bytes.Equal(recChunk.Data(), chunk.Data()) {
|
if !bytes.Equal(recChunk.Address(), chunk.Address()) || !bytes.Equal(recChunk.Data(), chunk.Data()) {
|
||||||
t.Fatalf("Different chunk received than what was put")
|
t.Fatalf("Different chunk received than what was put")
|
||||||
|
|
@ -200,14 +211,18 @@ func TestNetStoreGetTimeout(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := make(chan struct{}) // this channel ensures that the gouroutine does not run earlier than the Get
|
c := make(chan struct{}) // this channel ensures that the gouroutine does not run earlier than the Get
|
||||||
|
fetcherErrC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
<-c // wait for the Get to be called
|
<-c // wait for the Get to be called
|
||||||
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
||||||
|
|
||||||
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
||||||
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
||||||
t.Fatal("Expected netStore to use a fetcher for the Get call")
|
fetcherErrC <- errors.New("Expected netStore to use a fetcher for the Get call")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetcherErrC <- nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
close(c)
|
close(c)
|
||||||
|
|
@ -220,6 +235,10 @@ func TestNetStoreGetTimeout(t *testing.T) {
|
||||||
t.Fatalf("Expected context.DeadLineExceeded err got %v", err)
|
t.Fatalf("Expected context.DeadLineExceeded err got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := <-fetcherErrC; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// A fetcher was created, check if it has been removed after timeout
|
// A fetcher was created, check if it has been removed after timeout
|
||||||
if netStore.fetchers.Len() != 0 {
|
if netStore.fetchers.Len() != 0 {
|
||||||
t.Fatal("Expected netStore to remove the fetcher after timeout")
|
t.Fatal("Expected netStore to remove the fetcher after timeout")
|
||||||
|
|
@ -243,20 +262,29 @@ func TestNetStoreGetCancel(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
|
|
||||||
c := make(chan struct{}) // this channel ensures that the gouroutine with the cancel does not run earlier than the Get
|
c := make(chan struct{}) // this channel ensures that the gouroutine with the cancel does not run earlier than the Get
|
||||||
|
fetcherErrC := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
<-c // wait for the Get to be called
|
<-c // wait for the Get to be called
|
||||||
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
time.Sleep(200 * time.Millisecond) // and a little more so it is surely called
|
||||||
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
// check if netStore created a fetcher in the Get call for the unavailable chunk
|
||||||
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
if netStore.fetchers.Len() != 1 || netStore.getFetcher(chunk.Address()) == nil {
|
||||||
t.Fatal("Expected netStore to use a fetcher for the Get call")
|
fetcherErrC <- errors.New("Expected netStore to use a fetcher for the Get call")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetcherErrC <- nil
|
||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
close(c)
|
close(c)
|
||||||
|
|
||||||
// We call Get with an unavailable chunk, so it will create a fetcher and wait for delivery
|
// We call Get with an unavailable chunk, so it will create a fetcher and wait for delivery
|
||||||
_, err := netStore.Get(ctx, chunk.Address())
|
_, err := netStore.Get(ctx, chunk.Address())
|
||||||
|
|
||||||
|
if err := <-fetcherErrC; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// After the context is cancelled above Get should return with an error
|
// After the context is cancelled above Get should return with an error
|
||||||
if err != context.Canceled {
|
if err != context.Canceled {
|
||||||
t.Fatalf("Expected context.Canceled err got %v", err)
|
t.Fatalf("Expected context.Canceled err got %v", err)
|
||||||
|
|
@ -286,46 +314,55 @@ func TestNetStoreMultipleGetAndPut(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
putErrC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
// sleep to make sure Put is called after all the Get
|
// sleep to make sure Put is called after all the Get
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
// check if netStore created exactly one fetcher for all Get calls
|
// check if netStore created exactly one fetcher for all Get calls
|
||||||
if netStore.fetchers.Len() != 1 {
|
if netStore.fetchers.Len() != 1 {
|
||||||
t.Fatal("Expected netStore to use one fetcher for all Get calls")
|
putErrC <- errors.New("Expected netStore to use one fetcher for all Get calls")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
err := netStore.Put(ctx, chunk)
|
err := netStore.Put(ctx, chunk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no err got %v", err)
|
putErrC <- fmt.Errorf("Expected no err got %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
putErrC <- nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
count := 4
|
||||||
// call Get 4 times for the same unavailable chunk. The calls will be blocked until the Put above.
|
// call Get 4 times for the same unavailable chunk. The calls will be blocked until the Put above.
|
||||||
getWG := sync.WaitGroup{}
|
errC := make(chan error)
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < count; i++ {
|
||||||
getWG.Add(1)
|
|
||||||
go func() {
|
go func() {
|
||||||
defer getWG.Done()
|
|
||||||
recChunk, err := netStore.Get(ctx, chunk.Address())
|
recChunk, err := netStore.Get(ctx, chunk.Address())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no err got %v", err)
|
errC <- fmt.Errorf("Expected no err got %v", err)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(recChunk.Address(), chunk.Address()) || !bytes.Equal(recChunk.Data(), chunk.Data()) {
|
if !bytes.Equal(recChunk.Address(), chunk.Address()) || !bytes.Equal(recChunk.Data(), chunk.Data()) {
|
||||||
t.Fatalf("Different chunk received than what was put")
|
errC <- errors.New("Different chunk received than what was put")
|
||||||
}
|
}
|
||||||
|
errC <- nil
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
finishedC := make(chan struct{})
|
if err := <-putErrC; err != nil {
|
||||||
go func() {
|
t.Fatal(err)
|
||||||
getWG.Wait()
|
}
|
||||||
close(finishedC)
|
|
||||||
}()
|
timeout := time.After(1 * time.Second)
|
||||||
|
|
||||||
// The Get calls should return after Put, so no timeout expected
|
// The Get calls should return after Put, so no timeout expected
|
||||||
select {
|
for i := 0; i < count; i++ {
|
||||||
case <-finishedC:
|
select {
|
||||||
case <-time.After(1 * time.Second):
|
case err := <-errC:
|
||||||
t.Fatalf("Timeout waiting for Get calls to return")
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case <-timeout:
|
||||||
|
t.Fatalf("Timeout waiting for Get calls to return")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A fetcher was created, check if it has been removed after cancel
|
// A fetcher was created, check if it has been removed after cancel
|
||||||
|
|
@ -448,7 +485,7 @@ func TestNetStoreGetCallsOffer(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// We call get for a not available chunk, it will timeout because the chunk is not delivered
|
// We call get for a not available chunk, it will timeout because the chunk is not delivered
|
||||||
chunk, err := netStore.Get(ctx, chunk.Address())
|
_, err := netStore.Get(ctx, chunk.Address())
|
||||||
|
|
||||||
if err != context.DeadlineExceeded {
|
if err != context.DeadlineExceeded {
|
||||||
t.Fatalf("Expect error %v got %v", context.DeadlineExceeded, err)
|
t.Fatalf("Expect error %v got %v", context.DeadlineExceeded, err)
|
||||||
|
|
@ -542,16 +579,12 @@ func TestNetStoreFetchFuncCalledMultipleTimes(t *testing.T) {
|
||||||
t.Fatalf("Expected netStore to have one fetcher for the requested chunk")
|
t.Fatalf("Expected netStore to have one fetcher for the requested chunk")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call wait three times parallelly
|
// Call wait three times in parallel
|
||||||
wg := sync.WaitGroup{}
|
count := 3
|
||||||
for i := 0; i < 3; i++ {
|
errC := make(chan error)
|
||||||
wg.Add(1)
|
for i := 0; i < count; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
err := wait(ctx)
|
errC <- wait(ctx)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Expected no err got %v", err)
|
|
||||||
}
|
|
||||||
wg.Done()
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -570,7 +603,12 @@ func TestNetStoreFetchFuncCalledMultipleTimes(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until all wait calls return (because the chunk is delivered)
|
// wait until all wait calls return (because the chunk is delivered)
|
||||||
wg.Wait()
|
for i := 0; i < count; i++ {
|
||||||
|
err := <-errC
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// There should be no more fetchers for the delivered chunk
|
// There should be no more fetchers for the delivered chunk
|
||||||
if netStore.fetchers.Len() != 0 {
|
if netStore.fetchers.Len() != 0 {
|
||||||
|
|
@ -606,23 +644,29 @@ func TestNetStoreFetcherLifeCycleWithTimeout(t *testing.T) {
|
||||||
t.Fatalf("Expected netStore to have one fetcher for the requested chunk")
|
t.Fatalf("Expected netStore to have one fetcher for the requested chunk")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call wait three times parallelly
|
// Call wait three times in parallel
|
||||||
wg := sync.WaitGroup{}
|
count := 3
|
||||||
for i := 0; i < 3; i++ {
|
errC := make(chan error)
|
||||||
wg.Add(1)
|
for i := 0; i < count; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
|
||||||
rctx, rcancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
rctx, rcancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||||
defer rcancel()
|
defer rcancel()
|
||||||
err := wait(rctx)
|
err := wait(rctx)
|
||||||
if err != context.DeadlineExceeded {
|
if err != context.DeadlineExceeded {
|
||||||
t.Fatalf("Expected err %v got %v", context.DeadlineExceeded, err)
|
errC <- fmt.Errorf("Expected err %v got %v", context.DeadlineExceeded, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
errC <- nil
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until all wait calls timeout
|
// wait until all wait calls timeout
|
||||||
wg.Wait()
|
for i := 0; i < count; i++ {
|
||||||
|
err := <-errC
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// There should be no more fetchers after timeout
|
// There should be no more fetchers after timeout
|
||||||
if netStore.fetchers.Len() != 0 {
|
if netStore.fetchers.Len() != 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue