asyncloader_test.go 5.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package asyncloader

import (
	"context"
	"fmt"
	"math/rand"
	"testing"
	"time"

	gsmsg "github.com/ipfs/go-graphsync/message"
	"github.com/ipfs/go-graphsync/testbridge"
	"github.com/ipfs/go-graphsync/testutil"
	ipld "github.com/ipld/go-ipld-prime"
)

func TestAsyncLoadWhenRequestNotInProgress(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		callCount++
		return testutil.RandomBytes(100), nil
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
30
	resultChan := asyncLoader.AsyncLoad(requestID, link)
31 32

	select {
33 34
	case result := <-resultChan:
		if result.Data != nil {
35 36
			t.Fatal("should not have sent responses")
		}
37
		if result.Err == nil {
38
			t.Fatal("should have sent an error")
39

40 41
		}
	case <-ctx.Done():
42
		t.Fatal("should have produced result")
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	}

	if callCount > 0 {
		t.Fatal("should not have attempted to load link but did")
	}
}

func TestAsyncLoadWhenInitialLoadSucceeds(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		callCount++
		return testutil.RandomBytes(100), nil
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	asyncLoader.StartRequest(requestID)
65
	resultChan := asyncLoader.AsyncLoad(requestID, link)
66 67

	select {
68 69
	case result := <-resultChan:
		if result.Data == nil {
70 71
			t.Fatal("should have sent a response")
		}
72
		if result.Err != nil {
73 74 75
			t.Fatal("should not have sent an error")
		}
	case <-ctx.Done():
76
		t.Fatal("should have closed response channel")
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	}

	if callCount == 0 {
		t.Fatal("should have attempted to load link but did not")
	}
}

func TestAsyncLoadInitialLoadFails(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		callCount++
		return nil, fmt.Errorf("something went wrong")
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	asyncLoader.StartRequest(requestID)
99
	resultChan := asyncLoader.AsyncLoad(requestID, link)
100 101

	select {
102 103
	case result := <-resultChan:
		if result.Data != nil {
104 105
			t.Fatal("should not have sent responses")
		}
106
		if result.Err == nil {
107 108 109
			t.Fatal("should have sent an error")
		}
	case <-ctx.Done():
110
		t.Fatal("should have closed response channel")
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}

	if callCount == 0 {
		t.Fatal("should have attempted to load link but did not")
	}
}

func TestAsyncLoadInitialLoadIndeterminateThenSucceeds(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	called := make(chan struct{}, 2)
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		var result []byte
		called <- struct{}{}
		if callCount > 0 {
			result = testutil.RandomBytes(100)
		}
		callCount++
		return result, nil
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	asyncLoader.StartRequest(requestID)
139
	resultChan := asyncLoader.AsyncLoad(requestID, link)
140 141
	select {
	case <-called:
142
	case <-resultChan:
143 144 145 146 147 148 149
		t.Fatal("Should not have sent message on response chan")
	case <-ctx.Done():
		t.Fatal("should have attempted load once")
	}
	asyncLoader.NewResponsesAvailable()

	select {
150 151
	case result := <-resultChan:
		if result.Data == nil {
152 153
			t.Fatal("should have sent a response")
		}
154
		if result.Err != nil {
155 156 157
			t.Fatal("should not have sent an error")
		}
	case <-ctx.Done():
158
		t.Fatal("should have closed response channel")
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	}

	if callCount < 2 {
		t.Fatal("should have attempted to load multiple times till success but did not")
	}
}

func TestAsyncLoadInitialLoadIndeterminateThenRequestFinishes(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	called := make(chan struct{}, 2)
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		var result []byte
		called <- struct{}{}
		if callCount > 0 {
			result = testutil.RandomBytes(100)
		}
		callCount++
		return result, nil
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	asyncLoader.StartRequest(requestID)
187
	resultChan := asyncLoader.AsyncLoad(requestID, link)
188 189
	select {
	case <-called:
190
	case <-resultChan:
191 192 193 194 195 196 197 198
		t.Fatal("Should not have sent message on response chan")
	case <-ctx.Done():
		t.Fatal("should have attempted load once")
	}
	asyncLoader.FinishRequest(requestID)
	asyncLoader.NewResponsesAvailable()

	select {
199 200
	case result := <-resultChan:
		if result.Data != nil {
201 202
			t.Fatal("should not have sent responses")
		}
203
		if result.Err == nil {
204 205 206
			t.Fatal("should have sent an error")
		}
	case <-ctx.Done():
207
		t.Fatal("should have closed response channel")
208
	}
209

210 211 212 213
	if callCount > 1 {
		t.Fatal("should only have attempted one call but attempted multiple")
	}
}