asyncloader_test.go 5.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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"
)

16
func TestAsyncLoadInitialLoadSucceeds(t *testing.T) {
17 18 19 20 21 22 23 24 25 26 27 28 29
	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
	case result := <-resultChan:
34 35
		if result.Data == nil {
			t.Fatal("should have sent a response")
36
		}
37 38
		if result.Err != nil {
			t.Fatal("should not have sent an error")
39 40
		}
	case <-ctx.Done():
41
		t.Fatal("should have closed response channel")
42 43
	}

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

49
func TestAsyncLoadInitialLoadFails(t *testing.T) {
50 51 52 53 54 55
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
		callCount++
56
		return nil, fmt.Errorf("something went wrong")
57 58 59 60 61 62
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
63
	resultChan := asyncLoader.AsyncLoad(requestID, link)
64 65

	select {
66
	case result := <-resultChan:
67 68
		if result.Data != nil {
			t.Fatal("should not have sent responses")
69
		}
70 71
		if result.Err == nil {
			t.Fatal("should have sent an error")
72 73
		}
	case <-ctx.Done():
74
		t.Fatal("should have closed response channel")
75 76 77 78 79 80
	}

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

81 82
}
func TestAsyncLoadInitialLoadIndeterminateWhenRequestNotInProgress(t *testing.T) {
83 84 85 86 87
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
	defer cancel()
	callCount := 0
	loadAttempter := func(gsmsg.GraphSyncRequestID, ipld.Link) ([]byte, error) {
88 89 90 91
		var result []byte
		if callCount > 0 {
			result = testutil.RandomBytes(100)
		}
92
		callCount++
93
		return result, nil
94 95 96 97 98 99
	}
	asyncLoader := New(ctx, loadAttempter)
	asyncLoader.Startup()

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

	select {
103 104
	case result := <-resultChan:
		if result.Data != nil {
105 106
			t.Fatal("should not have sent responses")
		}
107
		if result.Err == nil {
108
			t.Fatal("should have sent an error")
109

110 111
		}
	case <-ctx.Done():
112
		t.Fatal("should have produced result")
113 114
	}

115 116
	if callCount > 1 {
		t.Fatal("should have failed after load with indeterminate result")
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	}
}

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)
141
	resultChan := asyncLoader.AsyncLoad(requestID, link)
142 143
	select {
	case <-called:
144
	case <-resultChan:
145 146 147 148 149 150 151
		t.Fatal("Should not have sent message on response chan")
	case <-ctx.Done():
		t.Fatal("should have attempted load once")
	}
	asyncLoader.NewResponsesAvailable()

	select {
152 153
	case result := <-resultChan:
		if result.Data == nil {
154 155
			t.Fatal("should have sent a response")
		}
156
		if result.Err != nil {
157 158 159
			t.Fatal("should not have sent an error")
		}
	case <-ctx.Done():
160
		t.Fatal("should have closed response channel")
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 187 188
	}

	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)
189
	resultChan := asyncLoader.AsyncLoad(requestID, link)
190 191
	select {
	case <-called:
192
	case <-resultChan:
193 194 195 196 197 198 199 200
		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 {
201 202
	case result := <-resultChan:
		if result.Data != nil {
203 204
			t.Fatal("should not have sent responses")
		}
205
		if result.Err == nil {
206 207 208
			t.Fatal("should have sent an error")
		}
	case <-ctx.Done():
209
		t.Fatal("should have closed response channel")
210
	}
211

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