loadattemptqueue_test.go 5.68 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
package loadattemptqueue

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

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

func TestAsyncLoadInitialLoadSucceeds(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
	}
	loadAttemptQueue := New(loadAttempter)

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())

	resultChan := make(chan types.AsyncLoadResult, 1)
	lr := NewLoadRequest(requestID, link, resultChan)
	loadAttemptQueue.AttemptLoad(lr, false)

	select {
	case result := <-resultChan:
		if result.Data == nil {
			t.Fatal("should have sent a response")
		}
		if result.Err != nil {
			t.Fatal("should not have sent an error")
		}
	case <-ctx.Done():
		t.Fatal("should have closed response channel")
	}

	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")
	}
	loadAttemptQueue := New(loadAttempter)

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	resultChan := make(chan types.AsyncLoadResult, 1)
	lr := NewLoadRequest(requestID, link, resultChan)
	loadAttemptQueue.AttemptLoad(lr, false)

	select {
	case result := <-resultChan:
		if result.Data != nil {
			t.Fatal("should not have sent responses")
		}
		if result.Err == nil {
			t.Fatal("should have sent an error")
		}
	case <-ctx.Done():
		t.Fatal("should have closed response channel")
	}

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

}

func TestAsyncLoadInitialLoadIndeterminateRetryFalse(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) {
		var result []byte
		if callCount > 0 {
			result = testutil.RandomBytes(100)
		}
		callCount++
		return result, nil
	}

	loadAttemptQueue := New(loadAttempter)

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	resultChan := make(chan types.AsyncLoadResult, 1)
	lr := NewLoadRequest(requestID, link, resultChan)
	loadAttemptQueue.AttemptLoad(lr, false)

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

		}
	case <-ctx.Done():
		t.Fatal("should have produced result")
	}

	if callCount > 1 {
		t.Fatal("should have failed after load with indeterminate result")
	}
}

func TestAsyncLoadInitialLoadIndeterminateRetryTrueThenRetriedSuccess(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
	}
	loadAttemptQueue := New(loadAttempter)

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	resultChan := make(chan types.AsyncLoadResult, 1)
	lr := NewLoadRequest(requestID, link, resultChan)
	loadAttemptQueue.AttemptLoad(lr, true)

	select {
	case <-called:
	case <-resultChan:
		t.Fatal("Should not have sent message on response chan")
	case <-ctx.Done():
		t.Fatal("should have attempted load once")
	}
	loadAttemptQueue.RetryLoads()

	select {
	case result := <-resultChan:
		if result.Data == nil {
			t.Fatal("should have sent a response")
		}
		if result.Err != nil {
			t.Fatal("should not have sent an error")
		}
	case <-ctx.Done():
		t.Fatal("should have closed response channel")
	}

	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
	}
	loadAttemptQueue := New(loadAttempter)

	link := testbridge.NewMockLink()
	requestID := gsmsg.GraphSyncRequestID(rand.Int31())
	resultChan := make(chan types.AsyncLoadResult, 1)
	lr := NewLoadRequest(requestID, link, resultChan)
	loadAttemptQueue.AttemptLoad(lr, true)

	select {
	case <-called:
	case <-resultChan:
		t.Fatal("Should not have sent message on response chan")
	case <-ctx.Done():
		t.Fatal("should have attempted load once")
	}
	loadAttemptQueue.ClearRequest(requestID)
	loadAttemptQueue.RetryLoads()

	select {
	case result := <-resultChan:
		if result.Data != nil {
			t.Fatal("should not have sent responses")
		}
		if result.Err == nil {
			t.Fatal("should have sent an error")
		}
	case <-ctx.Done():
		t.Fatal("should have closed response channel")
	}

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