github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/localstore/subscription_push_test.go (about)

     1  // Copyright 2019 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package localstore
    19  
    20  import (
    21  	"bytes"
    22  	"context"
    23  	"fmt"
    24  	"sync"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/swarm/storage"
    29  )
    30  
    31  // TestDB_SubscribePush uploads some chunks before and after
    32  // push syncing subscription is created and validates if
    33  // all addresses are received in the right order.
    34  func TestDB_SubscribePush(t *testing.T) {
    35  	db, cleanupFunc := newTestDB(t, nil)
    36  	defer cleanupFunc()
    37  
    38  	uploader := db.NewPutter(ModePutUpload)
    39  
    40  	chunks := make([]storage.Chunk, 0)
    41  	var chunksMu sync.Mutex
    42  
    43  	uploadRandomChunks := func(count int) {
    44  		for i := 0; i < count; i++ {
    45  			chunk := generateRandomChunk()
    46  
    47  			err := uploader.Put(chunk)
    48  			if err != nil {
    49  				t.Fatal(err)
    50  			}
    51  
    52  			chunksMu.Lock()
    53  			chunks = append(chunks, chunk)
    54  			chunksMu.Unlock()
    55  		}
    56  	}
    57  
    58  	// prepopulate database with some chunks
    59  	// before the subscription
    60  	uploadRandomChunks(10)
    61  
    62  	// set a timeout on subscription
    63  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    64  	defer cancel()
    65  
    66  	// collect all errors from validating addresses, even nil ones
    67  	// to validate the number of addresses received by the subscription
    68  	errChan := make(chan error)
    69  
    70  	ch, stop := db.SubscribePush(ctx)
    71  	defer stop()
    72  
    73  	// receive and validate addresses from the subscription
    74  	go func() {
    75  		var i int // address index
    76  		for {
    77  			select {
    78  			case got, ok := <-ch:
    79  				if !ok {
    80  					return
    81  				}
    82  				chunksMu.Lock()
    83  				want := chunks[i]
    84  				chunksMu.Unlock()
    85  				var err error
    86  				if !bytes.Equal(got.Data(), want.Data()) {
    87  					err = fmt.Errorf("got chunk %v data %x, want %x", i, got.Data(), want.Data())
    88  				}
    89  				if !bytes.Equal(got.Address(), want.Address()) {
    90  					err = fmt.Errorf("got chunk %v address %s, want %s", i, got.Address().Hex(), want.Address().Hex())
    91  				}
    92  				i++
    93  				// send one and only one error per received address
    94  				errChan <- err
    95  			case <-ctx.Done():
    96  				return
    97  			}
    98  		}
    99  	}()
   100  
   101  	// upload some chunks just after subscribe
   102  	uploadRandomChunks(5)
   103  
   104  	time.Sleep(200 * time.Millisecond)
   105  
   106  	// upload some chunks after some short time
   107  	// to ensure that subscription will include them
   108  	// in a dynamic environment
   109  	uploadRandomChunks(3)
   110  
   111  	checkErrChan(ctx, t, errChan, len(chunks))
   112  }
   113  
   114  // TestDB_SubscribePush_multiple uploads chunks before and after
   115  // multiple push syncing subscriptions are created and
   116  // validates if all addresses are received in the right order.
   117  func TestDB_SubscribePush_multiple(t *testing.T) {
   118  	db, cleanupFunc := newTestDB(t, nil)
   119  	defer cleanupFunc()
   120  
   121  	uploader := db.NewPutter(ModePutUpload)
   122  
   123  	addrs := make([]storage.Address, 0)
   124  	var addrsMu sync.Mutex
   125  
   126  	uploadRandomChunks := func(count int) {
   127  		for i := 0; i < count; i++ {
   128  			chunk := generateRandomChunk()
   129  
   130  			err := uploader.Put(chunk)
   131  			if err != nil {
   132  				t.Fatal(err)
   133  			}
   134  
   135  			addrsMu.Lock()
   136  			addrs = append(addrs, chunk.Address())
   137  			addrsMu.Unlock()
   138  		}
   139  	}
   140  
   141  	// prepopulate database with some chunks
   142  	// before the subscription
   143  	uploadRandomChunks(10)
   144  
   145  	// set a timeout on subscription
   146  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
   147  	defer cancel()
   148  
   149  	// collect all errors from validating addresses, even nil ones
   150  	// to validate the number of addresses received by the subscription
   151  	errChan := make(chan error)
   152  
   153  	subsCount := 10
   154  
   155  	// start a number of subscriptions
   156  	// that all of them will write every addresses error to errChan
   157  	for j := 0; j < subsCount; j++ {
   158  		ch, stop := db.SubscribePush(ctx)
   159  		defer stop()
   160  
   161  		// receive and validate addresses from the subscription
   162  		go func(j int) {
   163  			var i int // address index
   164  			for {
   165  				select {
   166  				case got, ok := <-ch:
   167  					if !ok {
   168  						return
   169  					}
   170  					addrsMu.Lock()
   171  					want := addrs[i]
   172  					addrsMu.Unlock()
   173  					var err error
   174  					if !bytes.Equal(got.Address(), want) {
   175  						err = fmt.Errorf("got chunk %v address on subscription %v %s, want %s", i, j, got, want)
   176  					}
   177  					i++
   178  					// send one and only one error per received address
   179  					errChan <- err
   180  				case <-ctx.Done():
   181  					return
   182  				}
   183  			}
   184  		}(j)
   185  	}
   186  
   187  	// upload some chunks just after subscribe
   188  	uploadRandomChunks(5)
   189  
   190  	time.Sleep(200 * time.Millisecond)
   191  
   192  	// upload some chunks after some short time
   193  	// to ensure that subscription will include them
   194  	// in a dynamic environment
   195  	uploadRandomChunks(3)
   196  
   197  	// number of addresses received by all subscriptions
   198  	wantedChunksCount := len(addrs) * subsCount
   199  
   200  	checkErrChan(ctx, t, errChan, wantedChunksCount)
   201  }