From 7e4b74bf5ec7e453185084b1567a100b89cac060 Mon Sep 17 00:00:00 2001
From: Jakub Sztandera <kubuxu@protonmail.ch>
Date: Mon, 10 Oct 2016 15:52:50 -0400
Subject: [PATCH] Change the test from being Has based to Put based

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
---
 blockservice/blockservice_test.go | 43 +++++++++++++++++--------------
 1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/blockservice/blockservice_test.go b/blockservice/blockservice_test.go
index 82244c1cd..d87a383e5 100644
--- a/blockservice/blockservice_test.go
+++ b/blockservice/blockservice_test.go
@@ -3,42 +3,47 @@ package blockservice
 import (
 	"testing"
 
+	"github.com/ipfs/go-ipfs/blocks"
 	"github.com/ipfs/go-ipfs/blocks/blockstore"
 	butil "github.com/ipfs/go-ipfs/blocks/blocksutil"
 	offline "github.com/ipfs/go-ipfs/exchange/offline"
 
-	cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
 	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
 	dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
 )
 
 func TestWriteThroughWorks(t *testing.T) {
-	dstore := dssync.MutexWrap(ds.NewMapDatastore())
-	bstore := HasFailingBlockstore{
-		blockstore.NewBlockstore(dstore),
-		t,
-		true,
+	bstore := &PutCountingBlockstore{
+		blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
+		0,
 	}
-	exch := offline.Exchange(bstore)
+	bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
+	exch := offline.Exchange(bstore2)
 	bserv := NewWriteThrough(bstore, exch)
 	bgen := butil.NewBlockGenerator()
 
-	bserv.AddBlock(bgen.Next())
+	block := bgen.Next()
+
+	t.Logf("PutCounter: %d", bstore.PutCounter)
+	bserv.AddBlock(block)
+	if bstore.PutCounter != 1 {
+		t.Fatalf("expected just one Put call, have: %d", bstore.PutCounter)
+	}
+
+	bserv.AddBlock(block)
+	if bstore.PutCounter != 2 {
+		t.Fatal("Put should have called again, should be 2 is: %d", bstore.PutCounter)
+	}
 }
 
-var _ blockstore.GCBlockstore = (*HasFailingBlockstore)(nil)
+var _ blockstore.GCBlockstore = (*PutCountingBlockstore)(nil)
 
-type HasFailingBlockstore struct {
+type PutCountingBlockstore struct {
 	blockstore.GCBlockstore
-	t    *testing.T
-	Fail bool
+	PutCounter int
 }
 
-func (bs HasFailingBlockstore) Has(k *cid.Cid) (bool, error) {
-	if bs.Fail {
-		bs.t.Fatal("Has shouldn't be called")
-		return false, nil
-	}
-	return bs.GCBlockstore.Has(k)
-
+func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
+	bs.PutCounter++
+	return bs.GCBlockstore.Put(block)
 }
-- 
GitLab