github.com/xxRanger/go-ethereum@v1.8.23/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/storage"
    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([]storage.Chunk, 0)
    40  	var chunksMu sync.Mutex
    41  
    42  	uploadRandomChunks := func(count int) {
    43  		for i := 0; i < count; i++ {
    44  			chunk := generateRandomChunk()
    45  
    46  			err := uploader.Put(chunk)
    47  			if err != nil {
    48  				t.Fatal(err)
    49  			}
    50  
    51  			chunksMu.Lock()
    52  			chunks = append(chunks, chunk)
    53  			chunksMu.Unlock()
    54  		}
    55  	}
    56  
    57  	// prepopulate database with some chunks
    58  	// before the subscription
    59  	uploadRandomChunks(10)
    60  
    61  	// set a timeout on subscription
    62  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    63  	defer cancel()
    64  
    65  	// collect all errors from validating addresses, even nil ones
    66  	// to validate the number of addresses received by the subscription
    67  	errChan := make(chan error)
    68  
    69  	ch, stop := db.SubscribePush(ctx)
    70  	defer stop()
    71  
    72  	// receive and validate addresses from the subscription
    73  	go func() {
    74  		var i int // address index
    75  		for {
    76  			select {
    77  			case got, ok := <-ch:
    78  				if !ok {
    79  					return
    80  				}
    81  				chunksMu.Lock()
    82  				want := chunks[i]
    83  				chunksMu.Unlock()
    84  				var err error
    85  				if !bytes.Equal(got.Data(), want.Data()) {
    86  					err = fmt.Errorf("got chunk %v data %x, want %x", i, got.Data(), want.Data())
    87  				}
    88  				if !bytes.Equal(got.Address(), want.Address()) {
    89  					err = fmt.Errorf("got chunk %v address %s, want %s", i, got.Address().Hex(), want.Address().Hex())
    90  				}
    91  				i++
    92  				// send one and only one error per received address
    93  				errChan <- err
    94  			case <-ctx.Done():
    95  				return
    96  			}
    97  		}
    98  	}()
    99  
   100  	// upload some chunks just after subscribe
   101  	uploadRandomChunks(5)
   102  
   103  	time.Sleep(200 * time.Millisecond)
   104  
   105  	// upload some chunks after some short time
   106  	// to ensure that subscription will include them
   107  	// in a dynamic environment
   108  	uploadRandomChunks(3)
   109  
   110  	checkErrChan(ctx, t, errChan, len(chunks))
   111  }
   112  
   113  // TestDB_SubscribePush_multiple uploads chunks before and after
   114  // multiple push syncing subscriptions are created and
   115  // validates if all addresses are received in the right order.
   116  func TestDB_SubscribePush_multiple(t *testing.T) {
   117  	db, cleanupFunc := newTestDB(t, nil)
   118  	defer cleanupFunc()
   119  
   120  	uploader := db.NewPutter(ModePutUpload)
   121  
   122  	addrs := make([]storage.Address, 0)
   123  	var addrsMu sync.Mutex
   124  
   125  	uploadRandomChunks := func(count int) {
   126  		for i := 0; i < count; i++ {
   127  			chunk := generateRandomChunk()
   128  
   129  			err := uploader.Put(chunk)
   130  			if err != nil {
   131  				t.Fatal(err)
   132  			}
   133  
   134  			addrsMu.Lock()
   135  			addrs = append(addrs, chunk.Address())
   136  			addrsMu.Unlock()
   137  		}
   138  	}
   139  
   140  	// prepopulate database with some chunks
   141  	// before the subscription
   142  	uploadRandomChunks(10)
   143  
   144  	// set a timeout on subscription
   145  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
   146  	defer cancel()
   147  
   148  	// collect all errors from validating addresses, even nil ones
   149  	// to validate the number of addresses received by the subscription
   150  	errChan := make(chan error)
   151  
   152  	subsCount := 10
   153  
   154  	// start a number of subscriptions
   155  	// that all of them will write every addresses error to errChan
   156  	for j := 0; j < subsCount; j++ {
   157  		ch, stop := db.SubscribePush(ctx)
   158  		defer stop()
   159  
   160  		// receive and validate addresses from the subscription
   161  		go func(j int) {
   162  			var i int // address index
   163  			for {
   164  				select {
   165  				case got, ok := <-ch:
   166  					if !ok {
   167  						return
   168  					}
   169  					addrsMu.Lock()
   170  					want := addrs[i]
   171  					addrsMu.Unlock()
   172  					var err error
   173  					if !bytes.Equal(got.Address(), want) {
   174  						err = fmt.Errorf("got chunk %v address on subscription %v %s, want %s", i, j, got, want)
   175  					}
   176  					i++
   177  					// send one and only one error per received address
   178  					errChan <- err
   179  				case <-ctx.Done():
   180  					return
   181  				}
   182  			}
   183  		}(j)
   184  	}
   185  
   186  	// upload some chunks just after subscribe
   187  	uploadRandomChunks(5)
   188  
   189  	time.Sleep(200 * time.Millisecond)
   190  
   191  	// upload some chunks after some short time
   192  	// to ensure that subscription will include them
   193  	// in a dynamic environment
   194  	uploadRandomChunks(3)
   195  
   196  	// number of addresses received by all subscriptions
   197  	wantedChunksCount := len(addrs) * subsCount
   198  
   199  	checkErrChan(ctx, t, errChan, wantedChunksCount)
   200  }