Skip to content

Commit

Permalink
Update version (#17)
Browse files Browse the repository at this point in the history
* Update version
* Remove redundant test code
* Rename test go file
* In tests, use linkproto prefix defined in schema
* Update README with license
  • Loading branch information
gammazero authored Apr 6, 2023
1 parent cd49ff2 commit 3476034
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 56 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
> IPNI library implementation in Go
This project provides a Golang implementation of indexer API, data definitions, and utilities that can be used to build and interact with indexing services.

## License
[SPDX-License-Identifier: Apache-2.0 OR MIT](LICENSE.md)
2 changes: 1 addition & 1 deletion dagsync/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestSyncFnHttp(t *testing.T) {

// Try to sync with a non-existing cid to chack that sync returns with err,
// and SyncFinished watcher does not get event.
cids, _ := test.RandomCids(1)
cids := test.RandomCids(1)
ctx, syncncl := context.WithTimeout(context.Background(), time.Second)
defer syncncl()

Expand Down
6 changes: 2 additions & 4 deletions dagsync/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func TestGetStopNodeWhenNil(t *testing.T) {
}

func TestGetRecursionLimit(t *testing.T) {
testCid, err := test.RandomCids(1)
require.NoError(t, err)
testCid := test.RandomCids(1)
testStopLink := cidlink.Link{Cid: testCid[0]}
ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
tests := []struct {
Expand Down Expand Up @@ -99,8 +98,7 @@ func TestGetRecursionLimit(t *testing.T) {
}

func TestWithRecursionLimit(t *testing.T) {
testCid, err := test.RandomCids(1)
require.NoError(t, err)
testCid := test.RandomCids(1)
testStopLink := cidlink.Link{Cid: testCid[0]}
ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions dagsync/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,14 @@ func (h *hostSystem) close() {
}

func (b dagsyncPubSubBuilder) Build(t *testing.T, topicName string, pubSys hostSystem, subSys hostSystem, subOpts []dagsync.Option) (dagsync.Publisher, *dagsync.Subscriber) {
var pub dagsync.Publisher
var senders []announce.Sender

if !b.P2PAnnounce {
p2pSender, err := p2psender.New(pubSys.host, topicName)
require.NoError(t, err)
senders = append(senders, p2pSender)
}

var pub dagsync.Publisher
var err error
if b.IsHttp {
pub, err = httpsync.NewPublisher("127.0.0.1:0", pubSys.lsys, pubSys.privKey, httpsync.WithAnnounceSenders(senders...))
Expand Down
2 changes: 1 addition & 1 deletion dagsync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestSyncFn(t *testing.T) {

// Try to sync with a non-existing cid to check that sync returns with err,
// and SyncFinished watcher does not get event.
cids, _ := test.RandomCids(1)
cids := test.RandomCids(1)
ctx, syncncl := context.WithTimeout(context.Background(), updateTimeout)
defer syncncl()
peerInfo := peer.AddrInfo{
Expand Down
49 changes: 10 additions & 39 deletions dagsync/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"path"
"sync"
Expand All @@ -20,13 +19,14 @@ import (
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipni/go-libipni/dagsync/p2p/protocol/head"
"github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/go-libipni/maurl"
"github.com/ipni/go-libipni/test"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -158,38 +158,15 @@ func waitForMeshWithMessage(t *testing.T, topic string, hosts ...host.Host) []*p
// just gimme a link and stuff the bytes in a map.
// (also return the node again for convenient assignment.)
func encode(lsys ipld.LinkSystem, n ipld.Node) (ipld.Node, ipld.Link) {
lp := cidlink.LinkPrototype{
Prefix: prefix,
}

lnk, err := lsys.Store(ipld.LinkContext{}, lp, n)
lnk, err := lsys.Store(ipld.LinkContext{}, schema.Linkproto, n)
if err != nil {
panic(err)
}
return n, lnk
}

var prefix = cid.Prefix{
Version: 1,
Codec: uint64(multicodec.DagJson),
MhType: uint64(multicodec.Sha2_256),
MhLength: 16,
}

func RandomCids(n int) ([]cid.Cid, error) {
var prng = rand.New(rand.NewSource(time.Now().UnixNano()))

res := make([]cid.Cid, n)
for i := 0; i < n; i++ {
b := make([]byte, 10*n)
prng.Read(b)
c, err := prefix.Sum(b)
if err != nil {
return nil, err
}
res[i] = c
}
return res, nil
func RandomCids(n int) []cid.Cid {
return test.RandomCids(n)
}

func MkLinkSystem(ds datastore.Batching) ipld.LinkSystem {
Expand All @@ -211,22 +188,16 @@ func MkLinkSystem(ds datastore.Batching) ipld.LinkSystem {
}

func Store(srcStore datastore.Batching, n ipld.Node) (ipld.Link, error) {
linkproto := cidlink.LinkPrototype{
Prefix: cid.Prefix{
Version: 1,
Codec: uint64(multicodec.DagJson),
MhType: uint64(multicodec.Sha2_256),
MhLength: 16,
},
}
lsys := MkLinkSystem(srcStore)

return lsys.Store(ipld.LinkContext{}, linkproto, n)
return lsys.Store(ipld.LinkContext{}, schema.Linkproto, n)
}

func MkTestHost(options ...libp2p.Option) host.Host {
options = append(options, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
h, _ := libp2p.New(options...)
h, err := libp2p.New(options...)
if err != nil {
panic(err)
}
return h
}

Expand Down
9 changes: 1 addition & 8 deletions test/util.go → test/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,7 @@ retry:
// RandomMultihashes returns a slice of n random unique Multihashes.
func RandomMultihashes(n int) []multihash.Multihash {
rng := rand.New(rand.NewSource(globalSeed.Add(1)))

prefix := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: multihash.SHA2_256,
MhLength: -1, // default length
}

prefix := schema.Linkproto.Prefix
set := make(map[string]struct{})
mhashes := make([]multihash.Multihash, n)
for i := 0; i < n; i++ {
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.0.4"
"version": "v0.0.5"
}

0 comments on commit 3476034

Please sign in to comment.