1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 05:52:20 +08:00

golint util/, thirdparty/

and exchange/bitswap/testutils.go

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
This commit is contained in:
rht
2015-06-14 21:44:32 +07:00
parent 2e9ec3deb8
commit 18f5fcea28
11 changed files with 36 additions and 38 deletions

View File

@ -50,7 +50,7 @@ func (g *SessionGenerator) Next() Instance {
} }
func (g *SessionGenerator) Instances(n int) []Instance { func (g *SessionGenerator) Instances(n int) []Instance {
instances := make([]Instance, 0) var instances []Instance
for j := 0; j < n; j++ { for j := 0; j < n; j++ {
inst := g.Next() inst := g.Next()
instances = append(instances, inst) instances = append(instances, inst)
@ -87,12 +87,12 @@ func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration {
// just a much better idea. // just a much better idea.
func session(ctx context.Context, net tn.Network, p testutil.Identity) Instance { func session(ctx context.Context, net tn.Network, p testutil.Identity) Instance {
bsdelay := delay.Fixed(0) bsdelay := delay.Fixed(0)
const kWriteCacheElems = 100 const writeCacheElems = 100
adapter := net.Adapter(p) adapter := net.Adapter(p)
dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay)) dstore := ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay))
bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)), kWriteCacheElems) bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds_sync.MutexWrap(dstore)), writeCacheElems)
if err != nil { if err != nil {
panic(err.Error()) // FIXME perhaps change signature and return error. panic(err.Error()) // FIXME perhaps change signature and return error.
} }

View File

@ -61,7 +61,7 @@ func TestDegenerateSlowRouting(t *testing.T) {
func Test100MBMacbookCoastToCoast(t *testing.T) { func Test100MBMacbookCoastToCoast(t *testing.T) {
SkipUnlessEpic(t) SkipUnlessEpic(t)
conf := testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() conf := testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow()
if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil { if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -21,7 +21,7 @@ func benchmarkAddCat(numBytes int64, conf testutil.LatencyConfig, b *testing.B)
} }
} }
var instant = testutil.LatencyConfig{}.All_Instantaneous() var instant = testutil.LatencyConfig{}.AllInstantaneous()
func BenchmarkInstantaneousAddCat1KB(b *testing.B) { benchmarkAddCat(1*unit.KB, instant, b) } func BenchmarkInstantaneousAddCat1KB(b *testing.B) { benchmarkAddCat(1*unit.KB, instant, b) }
func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, instant, b) } func BenchmarkInstantaneousAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, instant, b) }
@ -34,7 +34,7 @@ func BenchmarkInstantaneousAddCat64MB(b *testing.B) { benchmarkAddCat(64*unit.M
func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, instant, b) } func BenchmarkInstantaneousAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, instant, b) }
func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, instant, b) } func BenchmarkInstantaneousAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, instant, b) }
var routing = testutil.LatencyConfig{}.Routing_Slow() var routing = testutil.LatencyConfig{}.RoutingSlow()
func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, routing, b) } func BenchmarkRoutingSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, routing, b) }
func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, routing, b) } func BenchmarkRoutingSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, routing, b) }
@ -47,7 +47,7 @@ func BenchmarkRoutingSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB
func BenchmarkRoutingSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, routing, b) } func BenchmarkRoutingSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, routing, b) }
func BenchmarkRoutingSlowAddCat512MB(b *testing.B) { benchmarkAddCat(512*unit.MB, routing, b) } func BenchmarkRoutingSlowAddCat512MB(b *testing.B) { benchmarkAddCat(512*unit.MB, routing, b) }
var network = testutil.LatencyConfig{}.Network_NYtoSF() var network = testutil.LatencyConfig{}.NetworkNYtoSF()
func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, network, b) } func BenchmarkNetworkSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, network, b) }
func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, network, b) } func BenchmarkNetworkSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, network, b) }
@ -59,7 +59,7 @@ func BenchmarkNetworkSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*unit.MB,
func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, network, b) } func BenchmarkNetworkSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, network, b) }
func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, network, b) } func BenchmarkNetworkSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, network, b) }
var hdd = testutil.LatencyConfig{}.Blockstore_7200RPM() var hdd = testutil.LatencyConfig{}.Blockstore7200RPM()
func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat1MB(b *testing.B) { benchmarkAddCat(1*unit.MB, hdd, b) }
func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat2MB(b *testing.B) { benchmarkAddCat(2*unit.MB, hdd, b) }
@ -71,7 +71,7 @@ func BenchmarkBlockstoreSlowAddCat64MB(b *testing.B) { benchmarkAddCat(64*unit.
func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat128MB(b *testing.B) { benchmarkAddCat(128*unit.MB, hdd, b) }
func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, hdd, b) } func BenchmarkBlockstoreSlowAddCat256MB(b *testing.B) { benchmarkAddCat(256*unit.MB, hdd, b) }
var mixed = testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() var mixed = testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow()
func BenchmarkMixedAddCat1MBXX(b *testing.B) { benchmarkAddCat(1*unit.MB, mixed, b) } func BenchmarkMixedAddCat1MBXX(b *testing.B) { benchmarkAddCat(1*unit.MB, mixed, b) }
func BenchmarkMixedAddCat2MBXX(b *testing.B) { benchmarkAddCat(2*unit.MB, mixed, b) } func BenchmarkMixedAddCat2MBXX(b *testing.B) { benchmarkAddCat(2*unit.MB, mixed, b) }

View File

@ -55,7 +55,7 @@ func TestThreeLeggedCatDegenerateSlowRouting(t *testing.T) {
func TestThreeLeggedCat100MBMacbookCoastToCoast(t *testing.T) { func TestThreeLeggedCat100MBMacbookCoastToCoast(t *testing.T) {
SkipUnlessEpic(t) SkipUnlessEpic(t)
conf := testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow() conf := testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow()
if err := RunThreeLeggedCat(RandomBytes(100*unit.MB), conf); err != nil { if err := RunThreeLeggedCat(RandomBytes(100*unit.MB), conf); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -11,7 +11,7 @@ func (l LoggableMap) Loggable() map[string]interface{} {
return l return l
} }
// Loggable converts a func into a Loggable // LoggableF converts a func into a Loggable
type LoggableF func() map[string]interface{} type LoggableF func() map[string]interface{}
func (l LoggableF) Loggable() map[string]interface{} { func (l LoggableF) Loggable() map[string]interface{} {

View File

@ -71,7 +71,7 @@ func (n *Notifier) Notify(e Notifiee) {
n.mu.Unlock() n.mu.Unlock()
} }
// StopNotifying stops notifying Notifiee e. This function // StopNotify stops notifying Notifiee e. This function
// is meant to be called behind your own type-safe function(s): // is meant to be called behind your own type-safe function(s):
// //
// // generic function for pattern-following // // generic function for pattern-following

View File

@ -11,31 +11,31 @@ import (
query "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query" query "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
) )
var _ datastore.Datastore = &RedisDatastore{} var _ datastore.Datastore = &Datastore{}
var _ datastore.ThreadSafeDatastore = &RedisDatastore{} var _ datastore.ThreadSafeDatastore = &Datastore{}
var ErrInvalidType = errors.New("redis datastore: invalid type error. this datastore only supports []byte values") var ErrInvalidType = errors.New("redis datastore: invalid type error. this datastore only supports []byte values")
func NewExpiringDatastore(client *redis.Client, ttl time.Duration) (datastore.ThreadSafeDatastore, error) { func NewExpiringDatastore(client *redis.Client, ttl time.Duration) (datastore.ThreadSafeDatastore, error) {
return &RedisDatastore{ return &Datastore{
client: client, client: client,
ttl: ttl, ttl: ttl,
}, nil }, nil
} }
func NewDatastore(client *redis.Client) (datastore.ThreadSafeDatastore, error) { func NewDatastore(client *redis.Client) (datastore.ThreadSafeDatastore, error) {
return &RedisDatastore{ return &Datastore{
client: client, client: client,
}, nil }, nil
} }
type RedisDatastore struct { type Datastore struct {
mu sync.Mutex mu sync.Mutex
client *redis.Client client *redis.Client
ttl time.Duration ttl time.Duration
} }
func (ds *RedisDatastore) Put(key datastore.Key, value interface{}) error { func (ds *Datastore) Put(key datastore.Key, value interface{}) error {
ds.mu.Lock() ds.mu.Lock()
defer ds.mu.Unlock() defer ds.mu.Unlock()
@ -59,26 +59,26 @@ func (ds *RedisDatastore) Put(key datastore.Key, value interface{}) error {
return nil return nil
} }
func (ds *RedisDatastore) Get(key datastore.Key) (value interface{}, err error) { func (ds *Datastore) Get(key datastore.Key) (value interface{}, err error) {
ds.mu.Lock() ds.mu.Lock()
defer ds.mu.Unlock() defer ds.mu.Unlock()
return ds.client.Cmd("GET", key.String()).Bytes() return ds.client.Cmd("GET", key.String()).Bytes()
} }
func (ds *RedisDatastore) Has(key datastore.Key) (exists bool, err error) { func (ds *Datastore) Has(key datastore.Key) (exists bool, err error) {
ds.mu.Lock() ds.mu.Lock()
defer ds.mu.Unlock() defer ds.mu.Unlock()
return ds.client.Cmd("EXISTS", key.String()).Bool() return ds.client.Cmd("EXISTS", key.String()).Bool()
} }
func (ds *RedisDatastore) Delete(key datastore.Key) (err error) { func (ds *Datastore) Delete(key datastore.Key) (err error) {
ds.mu.Lock() ds.mu.Lock()
defer ds.mu.Unlock() defer ds.mu.Unlock()
return ds.client.Cmd("DEL", key.String()).Err return ds.client.Cmd("DEL", key.String()).Err
} }
func (ds *RedisDatastore) Query(q query.Query) (query.Results, error) { func (ds *Datastore) Query(q query.Query) (query.Results, error) {
return nil, errors.New("TODO implement query for redis datastore?") return nil, errors.New("TODO implement query for redis datastore?")
} }
func (ds *RedisDatastore) IsThreadSafe() {} func (ds *Datastore) IsThreadSafe() {}

View File

@ -19,7 +19,7 @@ func (i Information) String() string {
tmp := int64(i) tmp := int64(i)
// default // default
var d int64 = tmp var d = tmp
symbol := "B" symbol := "B"
switch { switch {

View File

@ -122,7 +122,7 @@ func (p *PeerNetParams) checkKeys() error {
return fmt.Errorf("sig verify failed: %s", err) return fmt.Errorf("sig verify failed: %s", err)
} }
if !sigok { if !sigok {
return fmt.Errorf("sig verify failed: sig invalid!") return fmt.Errorf("sig verify failed: sig invalid")
} }
return nil // ok. move along. return nil // ok. move along.

View File

@ -8,7 +8,7 @@ type LatencyConfig struct {
RoutingLatency time.Duration RoutingLatency time.Duration
} }
func (c LatencyConfig) All_Instantaneous() LatencyConfig { func (c LatencyConfig) AllInstantaneous() LatencyConfig {
// Could use a zero value but whatever. Consistency of interface // Could use a zero value but whatever. Consistency of interface
c.NetworkLatency = 0 c.NetworkLatency = 0
c.RoutingLatency = 0 c.RoutingLatency = 0
@ -16,33 +16,33 @@ func (c LatencyConfig) All_Instantaneous() LatencyConfig {
return c return c
} }
func (c LatencyConfig) Network_NYtoSF() LatencyConfig { func (c LatencyConfig) NetworkNYtoSF() LatencyConfig {
c.NetworkLatency = 20 * time.Millisecond c.NetworkLatency = 20 * time.Millisecond
return c return c
} }
func (c LatencyConfig) Network_IntraDatacenter2014() LatencyConfig { func (c LatencyConfig) NetworkIntraDatacenter2014() LatencyConfig {
c.NetworkLatency = 250 * time.Microsecond c.NetworkLatency = 250 * time.Microsecond
return c return c
} }
func (c LatencyConfig) Blockstore_FastSSD2014() LatencyConfig { func (c LatencyConfig) BlockstoreFastSSD2014() LatencyConfig {
const iops = 100000 const iops = 100000
c.BlockstoreLatency = (1 / iops) * time.Second c.BlockstoreLatency = (1 / iops) * time.Second
return c return c
} }
func (c LatencyConfig) Blockstore_SlowSSD2014() LatencyConfig { func (c LatencyConfig) BlockstoreSlowSSD2014() LatencyConfig {
c.BlockstoreLatency = 150 * time.Microsecond c.BlockstoreLatency = 150 * time.Microsecond
return c return c
} }
func (c LatencyConfig) Blockstore_7200RPM() LatencyConfig { func (c LatencyConfig) Blockstore7200RPM() LatencyConfig {
c.BlockstoreLatency = 8 * time.Millisecond c.BlockstoreLatency = 8 * time.Millisecond
return c return c
} }
func (c LatencyConfig) Routing_Slow() LatencyConfig { func (c LatencyConfig) RoutingSlow() LatencyConfig {
c.RoutingLatency = 200 * time.Millisecond c.RoutingLatency = 200 * time.Millisecond
return c return c
} }

View File

@ -1,4 +1,4 @@
// package util implements various utility functions used within ipfs // Package util implements various utility functions used within ipfs
// that do not currently have a better place to live. // that do not currently have a better place to live.
package util package util
@ -95,7 +95,7 @@ func GetenvBool(name string) bool {
return v == "true" || v == "t" || v == "1" return v == "true" || v == "t" || v == "1"
} }
// multiErr is a util to return multiple errors // MultiErr is a util to return multiple errors
type MultiErr []error type MultiErr []error
func (m MultiErr) Error() string { func (m MultiErr) Error() string {
@ -116,17 +116,15 @@ func (m MultiErr) Error() string {
func Partition(subject string, sep string) (string, string, string) { func Partition(subject string, sep string) (string, string, string) {
if i := strings.Index(subject, sep); i != -1 { if i := strings.Index(subject, sep); i != -1 {
return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):] return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):]
} else {
return subject, "", ""
} }
return subject, "", ""
} }
func RPartition(subject string, sep string) (string, string, string) { func RPartition(subject string, sep string) (string, string, string) {
if i := strings.LastIndex(subject, sep); i != -1 { if i := strings.LastIndex(subject, sep); i != -1 {
return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):] return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):]
} else {
return subject, "", ""
} }
return subject, "", ""
} }
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits // Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits