func TestAddressInBin(t *testing.T) {
	db := newTestDB(t, nil)
	for po := uint8(0); po < swarm.MaxPO; po++ {
		addr := db.addressInBin(po)
		got := db.po(addr)
		if got != po {
			t.Errorf("got po %v, want %v", got, po)
		}
	}
}
TestAddressInBin validates that function addressInBin
returns a valid address for every proximity order bin.
package localstore
import (
	"context"
	"fmt"
	"sync"
	"testing"
	"time"
	"github.com/ethsana/sana/pkg/shed"
	"github.com/ethsana/sana/pkg/storage"
	"github.com/ethsana/sana/pkg/swarm"
)
func TestDB_SubscribePull_first(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 100)
	bin := uint8(1)
	chunksInGivenBin := uint64(len(addrs[bin]))
	errc := make(chan error)
	since := chunksInGivenBin + 1
	go func() {
		ch, _, stop := db.SubscribePull(context.TODO(), bin, since, 0)
		defer stop()
		chnk := <-ch
		if chnk.BinID != since {
			errc <- fmt.Errorf("expected chunk.BinID to be %v , but got %v", since, chnk.BinID)
		} else {
			errc <- nil
		}
	}()
	time.Sleep(100 * time.Millisecond)
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 100)
	err := <-errc
	if err != nil {
		t.Fatal(err)
	}
}
func TestDB_SubscribePull(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 10)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	errChan := make(chan error)
	for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
		ch, _, stop := db.SubscribePull(ctx, bin, 0, 0)
		defer stop()
		go readPullSubscriptionBin(ctx, db, bin, ch, addrs, &addrsMu, errChan)
	}
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 5)
	time.Sleep(200 * time.Millisecond)
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 3)
	checkErrChan(ctx, t, errChan, wantedChunksCount)
}
func TestDB_SubscribePull_multiple(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 10)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	errChan := make(chan error)
	subsCount := 10
	for j := 0; j < subsCount; j++ {
		for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
			ch, _, stop := db.SubscribePull(ctx, bin, 0, 0)
			defer stop()
			go readPullSubscriptionBin(ctx, db, bin, ch, addrs, &addrsMu, errChan)
		}
	}
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 5)
	time.Sleep(200 * time.Millisecond)
	uploadRandomChunksBin(t, db, addrs, &addrsMu, &wantedChunksCount, 3)
	checkErrChan(ctx, t, errChan, wantedChunksCount*subsCount)
}
func TestDB_SubscribePull_since(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	binIDCounter := make(map[uint8]uint64)
	var binIDCounterMu sync.RWMutex
	uploadRandomChunks := func(count int, wanted bool) (first map[uint8]uint64) {
		addrsMu.Lock()
		defer addrsMu.Unlock()
		first = make(map[uint8]uint64)
		for i := 0; i < count; i++ {
			ch := generateTestRandomChunk()
			_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
			if err != nil {
				t.Fatal(err)
			}
			bin := db.po(ch.Address())
			binIDCounterMu.RLock()
			binIDCounter[bin]++
			binIDCounterMu.RUnlock()
			if wanted {
				if _, ok := addrs[bin]; !ok {
					addrs[bin] = make([]swarm.Address, 0)
				}
				addrs[bin] = append(addrs[bin], ch.Address())
				wantedChunksCount++
				if _, ok := first[bin]; !ok {
					first[bin] = binIDCounter[bin]
				}
			}
		}
		return first
	}
	uploadRandomChunks(30, false)
	first := uploadRandomChunks(25, true)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	errChan := make(chan error)
	for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
		since, ok := first[bin]
		if !ok {
			continue
		}
		ch, _, stop := db.SubscribePull(ctx, bin, since, 0)
		defer stop()
		go readPullSubscriptionBin(ctx, db, bin, ch, addrs, &addrsMu, errChan)
	}
	checkErrChan(ctx, t, errChan, wantedChunksCount)
}
func TestDB_SubscribePull_until(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	binIDCounter := make(map[uint8]uint64)
	var binIDCounterMu sync.RWMutex
	uploadRandomChunks := func(count int, wanted bool) (last map[uint8]uint64) {
		addrsMu.Lock()
		defer addrsMu.Unlock()
		last = make(map[uint8]uint64)
		for i := 0; i < count; i++ {
			ch := generateTestRandomChunk()
			_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
			if err != nil {
				t.Fatal(err)
			}
			bin := db.po(ch.Address())
			if _, ok := addrs[bin]; !ok {
				addrs[bin] = make([]swarm.Address, 0)
			}
			if wanted {
				addrs[bin] = append(addrs[bin], ch.Address())
				wantedChunksCount++
			}
			binIDCounterMu.RLock()
			binIDCounter[bin]++
			binIDCounterMu.RUnlock()
			last[bin] = binIDCounter[bin]
		}
		return last
	}
	last := uploadRandomChunks(30, true)
	uploadRandomChunks(25, false)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	errChan := make(chan error)
	for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
		until, ok := last[bin]
		if !ok {
			continue
		}
		ch, _, stop := db.SubscribePull(ctx, bin, 0, until)
		defer stop()
		go readPullSubscriptionBin(ctx, db, bin, ch, addrs, &addrsMu, errChan)
	}
	uploadRandomChunks(15, false)
	checkErrChan(ctx, t, errChan, wantedChunksCount)
}
func TestDB_SubscribePull_sinceAndUntil(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	var addrsMu sync.Mutex
	var wantedChunksCount int
	binIDCounter := make(map[uint8]uint64)
	var binIDCounterMu sync.RWMutex
	uploadRandomChunks := func(count int, wanted bool) (last map[uint8]uint64) {
		addrsMu.Lock()
		defer addrsMu.Unlock()
		last = make(map[uint8]uint64)
		for i := 0; i < count; i++ {
			ch := generateTestRandomChunk()
			_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
			if err != nil {
				t.Fatal(err)
			}
			bin := db.po(ch.Address())
			if _, ok := addrs[bin]; !ok {
				addrs[bin] = make([]swarm.Address, 0)
			}
			if wanted {
				addrs[bin] = append(addrs[bin], ch.Address())
				wantedChunksCount++
			}
			binIDCounterMu.RLock()
			binIDCounter[bin]++
			binIDCounterMu.RUnlock()
			last[bin] = binIDCounter[bin]
		}
		return last
	}
	upload1 := uploadRandomChunks(100, false)
	upload2 := uploadRandomChunks(100, true)
	uploadRandomChunks(8, false)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	errChan := make(chan error)
	for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
		since, ok := upload1[bin]
		if ok {
			since++
		}
		until, ok := upload2[bin]
		if !ok {
			continue
		}
		ch, _, stop := db.SubscribePull(ctx, bin, since, until)
		defer stop()
		go readPullSubscriptionBin(ctx, db, bin, ch, addrs, &addrsMu, errChan)
	}
	uploadRandomChunks(15, false)
	checkErrChan(ctx, t, errChan, wantedChunksCount)
}
func TestDB_SubscribePull_rangeOnRemovedChunks(t *testing.T) {
	db := newTestDB(t, nil)
	chunks := make(map[uint8][]storage.Descriptor)
	binIDCounter := make(map[uint8]uint64)
	const chunkCount = 1000
	for i := 0; i < chunkCount; i++ {
		ch := generateTestRandomChunk()
		_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
		if err != nil {
			t.Fatal(err)
		}
		bin := db.po(ch.Address())
		binIDCounter[bin]++
		binID := binIDCounter[bin]
		if _, ok := chunks[bin]; !ok {
			chunks[bin] = make([]storage.Descriptor, 0)
		}
		chunks[bin] = append(chunks[bin], storage.Descriptor{
			Address: ch.Address(),
			BinID:   binID,
		})
	}
	for bin := range chunks {
		count := len(chunks[bin])
		for i := 0; i < count/2; i++ {
			d := chunks[bin][0]
			if err := db.Set(context.Background(), storage.ModeSetRemove, d.Address); err != nil {
				t.Fatal(err)
			}
			chunks[bin] = chunks[bin][1:]
		}
	}
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	var checkedBins int
	for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
		if len(chunks[bin]) == 0 {
			continue
		}
		var since uint64
		until := chunks[bin][0].BinID - 1
		if until == 0 {
			continue
		}
		ch, _, stop := db.SubscribePull(ctx, bin, since, until)
		defer stop()
		select {
		case d, ok := <-ch:
			if !ok {
				break
			}
			if d.BinID > until {
				t.Errorf("got %v for bin %v, subscribed until bin id %v", d.BinID, bin, until)
			}
		case <-ctx.Done():
			t.Error(ctx.Err())
		}
		checkedBins++
	}
	if checkedBins == 0 {
		t.Fatal("test did not perform any checks")
	}
}
func uploadRandomChunksBin(t *testing.T, db *DB, addrs map[uint8][]swarm.Address, addrsMu *sync.Mutex, wantedChunksCount *int, count int) {
	addrsMu.Lock()
	defer addrsMu.Unlock()
	for i := 0; i < count; i++ {
		ch := generateTestRandomChunk()
		_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
		if err != nil {
			t.Fatal(err)
		}
		bin := db.po(ch.Address())
		if _, ok := addrs[bin]; !ok {
			addrs[bin] = make([]swarm.Address, 0)
		}
		addrs[bin] = append(addrs[bin], ch.Address())
		*wantedChunksCount++
	}
}
func readPullSubscriptionBin(ctx context.Context, db *DB, bin uint8, ch <-chan storage.Descriptor, addrs map[uint8][]swarm.Address, addrsMu *sync.Mutex, errChan chan error) {
	var i int  
	for {
		select {
		case got, ok := <-ch:
			if !ok {
				return
			}
			var err error
			addrsMu.Lock()
			if i+1 > len(addrs[bin]) {
				err = fmt.Errorf("got more chunk addresses %v, then expected %v, for bin %v", i+1, len(addrs[bin]), bin)
			} else {
				addr := addrs[bin][i]
				if !got.Address.Equal(addr) {
					err = fmt.Errorf("got chunk bin id %v in bin %v %v, want %v", i, bin, got.Address, addr)
				} else {
					var want shed.Item
					want, err = db.retrievalDataIndex.Get(shed.Item{
						Address: addr.Bytes(),
					})
					if err != nil {
						err = fmt.Errorf("got chunk (bin id %v in bin %v) from retrieval index %s: %v", i, bin, addrs[bin][i], err)
					} else if got.BinID != want.BinID {
						err = fmt.Errorf("got chunk bin id %v in bin %v %v, want %v", i, bin, got, want)
					}
				}
			}
			addrsMu.Unlock()
			i++
			select {
			case errChan <- err:
			case <-ctx.Done():
				return
			}
		case <-ctx.Done():
			return
		}
	}
}
func checkErrChan(ctx context.Context, t *testing.T, errChan chan error, wantedChunksCount int) {
	t.Helper()
	for i := 0; i < wantedChunksCount; i++ {
		select {
		case err := <-errChan:
			if err != nil {
				t.Error(err)
			}
		case <-ctx.Done():
			t.Fatal(ctx.Err())
		}
	}
}
func TestDB_LastPullSubscriptionBinID(t *testing.T) {
	db := newTestDB(t, nil)
	addrs := make(map[uint8][]swarm.Address)
	binIDCounter := make(map[uint8]uint64)
	var binIDCounterMu sync.RWMutex
	last := make(map[uint8]uint64)
	for _, count := range []int{1, 3, 10, 11, 100, 120} {
		for i := 0; i < count; i++ {
			ch := generateTestRandomChunk()
			_, err := db.Put(context.Background(), storage.ModePutUpload, ch)
			if err != nil {
				t.Fatal(err)
			}
			bin := db.po(ch.Address())
			if _, ok := addrs[bin]; !ok {
				addrs[bin] = make([]swarm.Address, 0)
			}
			addrs[bin] = append(addrs[bin], ch.Address())
			binIDCounterMu.RLock()
			binIDCounter[bin]++
			binIDCounterMu.RUnlock()
			last[bin] = binIDCounter[bin]
		}
		for bin := uint8(0); bin <= swarm.MaxPO; bin++ {
			want, ok := last[bin]
			got, err := db.LastPullSubscriptionBinID(bin)
			if ok {
				if err != nil {
					t.Errorf("got unexpected error value %v", err)
				}
			}
			if got != want {
				t.Errorf("got last bin id %v, want %v", got, want)
			}
		}
	}
}