github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/storagetest/pulse_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/platformpolicy"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"github.com/stretchr/testify/suite"
    31  )
    32  
    33  type pulseSuite struct {
    34  	suite.Suite
    35  
    36  	cm      *component.Manager
    37  	ctx     context.Context
    38  	cleaner func()
    39  
    40  	pulseTracker storage.PulseTracker
    41  }
    42  
    43  func NewPulseSuite() *pulseSuite {
    44  	return &pulseSuite{
    45  		Suite: suite.Suite{},
    46  	}
    47  }
    48  
    49  // Init and run suite
    50  func TestPulse(t *testing.T) {
    51  	suite.Run(t, NewPulseSuite())
    52  }
    53  
    54  func (s *pulseSuite) BeforeTest(suiteName, testName string) {
    55  	s.cm = &component.Manager{}
    56  	s.ctx = inslogger.TestContext(s.T())
    57  
    58  	db, cleaner := TmpDB(s.ctx, s.T())
    59  	s.pulseTracker = storage.NewPulseTracker()
    60  
    61  	s.cm.Inject(
    62  		platformpolicy.NewPlatformCryptographyScheme(),
    63  		db,
    64  		s.pulseTracker,
    65  	)
    66  
    67  	err := s.cm.Init(s.ctx)
    68  	if err != nil {
    69  		s.T().Error("ComponentManager init failed", err)
    70  	}
    71  	err = s.cm.Start(s.ctx)
    72  	if err != nil {
    73  		s.T().Error("ComponentManager start failed", err)
    74  	}
    75  
    76  	s.cleaner = cleaner
    77  }
    78  
    79  func (s *pulseSuite) AfterTest(suiteName, testName string) {
    80  	err := s.cm.Stop(s.ctx)
    81  	if err != nil {
    82  		s.T().Error("ComponentManager stop failed", err)
    83  	}
    84  	s.cleaner()
    85  }
    86  
    87  func (s *pulseSuite) TestDB_AddPulse_IncrementsSerialNumber() {
    88  	err := s.pulseTracker.AddPulse(s.ctx, core.Pulse{PulseNumber: 1})
    89  	require.NoError(s.T(), err)
    90  	pulse, err := s.pulseTracker.GetPulse(s.ctx, 1)
    91  	assert.Equal(s.T(), 2, pulse.SerialNumber)
    92  
    93  	err = s.pulseTracker.AddPulse(s.ctx, core.Pulse{PulseNumber: 2})
    94  	require.NoError(s.T(), err)
    95  	pulse, err = s.pulseTracker.GetPulse(s.ctx, 2)
    96  	assert.Equal(s.T(), 3, pulse.SerialNumber)
    97  
    98  	err = s.pulseTracker.AddPulse(s.ctx, core.Pulse{PulseNumber: 3})
    99  	require.NoError(s.T(), err)
   100  	pulse, err = s.pulseTracker.GetPulse(s.ctx, 3)
   101  	assert.Equal(s.T(), 4, pulse.SerialNumber)
   102  }