github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/storagetest/jet_test.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package storagetest
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/insolar/insolar/component"
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/instrumentation/inslogger"
    26  	"github.com/insolar/insolar/ledger/storage"
    27  	"github.com/insolar/insolar/ledger/storage/jet"
    28  	"github.com/insolar/insolar/platformpolicy"
    29  	"github.com/insolar/insolar/testutils"
    30  	"github.com/stretchr/testify/require"
    31  	"github.com/stretchr/testify/suite"
    32  )
    33  
    34  type jetSuite struct {
    35  	suite.Suite
    36  
    37  	cm      *component.Manager
    38  	ctx     context.Context
    39  	cleaner func()
    40  
    41  	dropStorage storage.DropStorage
    42  
    43  	jetID core.RecordID
    44  }
    45  
    46  func NewJetSuite() *jetSuite {
    47  	return &jetSuite{
    48  		Suite: suite.Suite{},
    49  	}
    50  }
    51  
    52  // Init and run suite
    53  func TestJet(t *testing.T) {
    54  	suite.Run(t, NewJetSuite())
    55  }
    56  
    57  func (s *jetSuite) BeforeTest(suiteName, testName string) {
    58  	s.cm = &component.Manager{}
    59  	s.ctx = inslogger.TestContext(s.T())
    60  	s.jetID = core.TODOJetID
    61  
    62  	db, cleaner := TmpDB(s.ctx, s.T())
    63  
    64  	s.cleaner = cleaner
    65  	s.dropStorage = storage.NewDropStorage(10)
    66  
    67  	s.cm.Inject(
    68  		platformpolicy.NewPlatformCryptographyScheme(),
    69  		db,
    70  		s.dropStorage,
    71  	)
    72  
    73  	err := s.cm.Init(s.ctx)
    74  	if err != nil {
    75  		s.T().Error("ComponentManager init failed", err)
    76  	}
    77  	err = s.cm.Start(s.ctx)
    78  	if err != nil {
    79  		s.T().Error("ComponentManager start failed", err)
    80  	}
    81  }
    82  
    83  func (s *jetSuite) AfterTest(suiteName, testName string) {
    84  	err := s.cm.Stop(s.ctx)
    85  	if err != nil {
    86  		s.T().Error("ComponentManager stop failed", err)
    87  	}
    88  	s.cleaner()
    89  }
    90  
    91  func addDropSizeToDB(
    92  	ctx context.Context,
    93  	t *testing.T,
    94  	dropStorage storage.DropStorage,
    95  	jetID core.RecordID,
    96  	dropSize uint64,
    97  ) {
    98  	dropSizeData := &jet.DropSize{
    99  		JetID:    jetID,
   100  		PulseNo:  core.FirstPulseNumber,
   101  		DropSize: dropSize,
   102  	}
   103  
   104  	cryptoServiceMock := testutils.NewCryptographyServiceMock(t)
   105  	cryptoServiceMock.SignFunc = func(p []byte) (r *core.Signature, r1 error) {
   106  		signature := core.SignatureFromBytes(nil)
   107  		return &signature, nil
   108  	}
   109  
   110  	hasher := testutils.NewPlatformCryptographyScheme().IntegrityHasher()
   111  	_, err := dropSizeData.WriteHashData(hasher)
   112  	require.NoError(t, err)
   113  
   114  	signature, err := cryptoServiceMock.Sign(hasher.Sum(nil))
   115  	require.NoError(t, err)
   116  
   117  	dropSizeData.Signature = signature.Bytes()
   118  
   119  	err = dropStorage.AddDropSize(ctx, dropSizeData)
   120  	require.NoError(t, err)
   121  }
   122  
   123  func findSize(testSize uint64, dropSizes []jet.DropSize) bool {
   124  	for _, ds := range dropSizes {
   125  		if ds.DropSize == testSize {
   126  			return true
   127  		}
   128  	}
   129  
   130  	return false
   131  }
   132  
   133  func (s *jetSuite) TestAddAndGetDropSize() {
   134  	dropSizes := []uint64{100, 200, 300, 400}
   135  
   136  	for _, size := range dropSizes {
   137  		addDropSizeToDB(s.ctx, s.T(), s.dropStorage, s.jetID, size)
   138  	}
   139  
   140  	dropSizeHistory, err := s.dropStorage.GetDropSizeHistory(s.ctx, s.jetID)
   141  	require.NoError(s.T(), err)
   142  
   143  	dropSizeArray := []jet.DropSize(dropSizeHistory)
   144  
   145  	require.Equal(s.T(), len(dropSizes), len(dropSizeArray))
   146  
   147  	for _, size := range dropSizes {
   148  		require.True(s.T(), findSize(size, dropSizeArray))
   149  	}
   150  }
   151  
   152  func (s *jetSuite) TestAddDropSizeAndIncreaseLimit() {
   153  	numElements := s.dropStorage.GetJetSizesHistoryDepth() * 2
   154  
   155  	for i := 0; i <= numElements; i++ {
   156  		addDropSizeToDB(s.ctx, s.T(), s.dropStorage, s.jetID, uint64(i))
   157  	}
   158  
   159  	dropSizeHistory, err := s.dropStorage.GetDropSizeHistory(s.ctx, s.jetID)
   160  	require.NoError(s.T(), err)
   161  
   162  	dropSizeArray := []jet.DropSize(dropSizeHistory)
   163  	require.Equal(s.T(), s.dropStorage.GetJetSizesHistoryDepth(), len(dropSizeArray))
   164  
   165  	for i := numElements; i > (numElements - s.dropStorage.GetJetSizesHistoryDepth()); i-- {
   166  		require.True(s.T(), findSize(uint64(i), dropSizeArray), "Couldn't find %d", i)
   167  	}
   168  }