github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/swarm/storage/localstore/subscription_push_test.go (about)

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