generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 137
provider: add a buffered KeyChanFunc. #870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7195b1a
provider: add a buffered KeyChanFunc.
hsanjuan 4b0aebd
provider: NewBufferedProvider: implement using chanqueue
hsanjuan 5ce2312
provider: windows-32bit tests fail due to deep recursion (fix)
hsanjuan e380c9f
Use latest chanqeue to read directly from pins channel
gammazero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/ipfs/boxo/blockservice" | ||
| "github.com/ipfs/boxo/blockstore" | ||
| "github.com/ipfs/boxo/exchange/offline" | ||
| bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice" | ||
| "github.com/ipfs/boxo/ipld/merkledag" | ||
| mdutils "github.com/ipfs/boxo/ipld/merkledag/test" | ||
| ipinner "github.com/ipfs/boxo/pinning/pinner" | ||
| "github.com/ipfs/boxo/pinning/pinner/dspinner" | ||
| "github.com/ipfs/go-datastore" | ||
| dssync "github.com/ipfs/go-datastore/sync" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestBufferedPinProvider checks that we can modify a pinset while reading | ||
| // from the provider, as all elements of the pinset have been placed in | ||
| // memory. | ||
| func TestBufferedPinProvider(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| // Setup | ||
| ds := dssync.MutexWrap(datastore.NewMapDatastore()) | ||
| bs := blockstore.NewBlockstore(ds) | ||
| bserv := blockservice.New(bs, offline.Exchange(bs)) | ||
| fetcher := bsfetcher.NewFetcherConfig(bserv) | ||
| dserv := merkledag.NewDAGService(bserv) | ||
| pinner, err := dspinner.New(ctx, ds, dserv) | ||
| require.NoError(t, err) | ||
| daggen := mdutils.NewDAGGenerator() | ||
| root, _, err := daggen.MakeDagNode(dserv.Add, 1, 64) | ||
| require.NoError(t, err) | ||
| root2, _, err := daggen.MakeDagNode(dserv.Add, 1, 64) | ||
| require.NoError(t, err) | ||
|
|
||
| // test with 0 pins to ensure things work. | ||
| zeroProv := NewPinnedProvider(false, pinner, fetcher) | ||
| zeroKeyChanF := NewBufferedProvider(zeroProv) | ||
| zeroPins, err := zeroKeyChanF(ctx) | ||
| require.NoError(t, err) | ||
| for range zeroPins { | ||
| t.Error("There should not be any pins") | ||
| } | ||
|
|
||
| // Pin the first DAG. | ||
| err = pinner.PinWithMode(ctx, root, ipinner.Recursive, "test") | ||
| require.NoError(t, err) | ||
|
|
||
| // Then open the keyChanF to read the pins. This should trigger the | ||
| // pin query, but we don't read from it, so in normal condiditions | ||
| // it would block. | ||
| pinProv := NewPinnedProvider(false, pinner, fetcher) | ||
| keyChanF := NewBufferedProvider(pinProv) | ||
| root1pins, err := keyChanF(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // Give time to buffer all the results as this is happening in the | ||
| // background. | ||
| time.Sleep(200 * time.Millisecond) | ||
|
|
||
| // If the previous query was blocking the pinset under a read-lock, | ||
| // we would not be able to write a second pin: | ||
| err = pinner.PinWithMode(ctx, root2, ipinner.Recursive, "test") | ||
| require.NoError(t, err) | ||
|
|
||
| // Now we trigger a second query. | ||
| pinProv2 := NewPinnedProvider(false, pinner, fetcher) | ||
| keyChanF2 := NewBufferedProvider(pinProv2) | ||
| root2pins, err := keyChanF2(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // And finally proceed to read pins. The second keyChan should contain | ||
| // both root and root2 pins, while the first keyChan contains only the | ||
| // elements from the first pin because they were all cached before the | ||
| // second pin happened. | ||
| root1count := 0 | ||
| root2count := 0 | ||
| for range root2pins { | ||
| root2count++ | ||
| } | ||
| for range root1pins { | ||
| root1count++ | ||
| } | ||
| require.Equal(t, 64, root1count, "first pin should have provided 2048 cids") | ||
| require.Equal(t, 64+64, root2count, "second pin should have provided 4096 cids") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.