github.com/braveheart12/just@v0.8.7/ledger/artifactmanager/metrics_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 artifactmanager
    18  
    19  import (
    20  	"context"
    21  	"regexp"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/gojuno/minimock"
    26  	"github.com/insolar/insolar/component"
    27  	"github.com/insolar/insolar/core"
    28  	"github.com/insolar/insolar/core/reply"
    29  	"github.com/insolar/insolar/instrumentation/inslogger"
    30  	"github.com/insolar/insolar/ledger/storage"
    31  	"github.com/insolar/insolar/ledger/storage/nodes"
    32  	"github.com/insolar/insolar/ledger/storage/storagetest"
    33  	"github.com/insolar/insolar/platformpolicy"
    34  	"github.com/insolar/insolar/testutils"
    35  	"github.com/stretchr/testify/assert"
    36  	"github.com/stretchr/testify/require"
    37  	"github.com/stretchr/testify/suite"
    38  
    39  	"github.com/insolar/insolar/core/message"
    40  	"github.com/insolar/insolar/testutils/testmetrics"
    41  )
    42  
    43  type metricSuite struct {
    44  	suite.Suite
    45  
    46  	cm      *component.Manager
    47  	ctx     context.Context
    48  	cleaner func()
    49  	db      storage.DBContext
    50  
    51  	scheme        core.PlatformCryptographyScheme
    52  	pulseTracker  storage.PulseTracker
    53  	nodeStorage   nodes.Accessor
    54  	objectStorage storage.ObjectStorage
    55  	jetStorage    storage.JetStorage
    56  	dropStorage   storage.DropStorage
    57  	genesisState  storage.GenesisState
    58  }
    59  
    60  func NewMetricSuite() *metricSuite {
    61  	return &metricSuite{
    62  		Suite: suite.Suite{},
    63  	}
    64  }
    65  
    66  // Init and run suite
    67  func TestMetricSuite(t *testing.T) {
    68  	suite.Run(t, NewMetricSuite())
    69  }
    70  
    71  func (s *metricSuite) BeforeTest(suiteName, testName string) {
    72  	s.cm = &component.Manager{}
    73  	s.ctx = inslogger.TestContext(s.T())
    74  
    75  	db, cleaner := storagetest.TmpDB(s.ctx, s.T())
    76  	s.cleaner = cleaner
    77  	s.db = db
    78  	s.scheme = testutils.NewPlatformCryptographyScheme()
    79  	s.jetStorage = storage.NewJetStorage()
    80  	s.nodeStorage = nodes.NewStorage()
    81  	s.pulseTracker = storage.NewPulseTracker()
    82  	s.objectStorage = storage.NewObjectStorage()
    83  	s.dropStorage = storage.NewDropStorage(10)
    84  	s.genesisState = storage.NewGenesisInitializer()
    85  
    86  	s.cm.Inject(
    87  		s.scheme,
    88  		s.db,
    89  		s.jetStorage,
    90  		s.nodeStorage,
    91  		s.pulseTracker,
    92  		s.objectStorage,
    93  		s.dropStorage,
    94  		s.genesisState,
    95  	)
    96  
    97  	err := s.cm.Init(s.ctx)
    98  	if err != nil {
    99  		s.T().Error("ComponentManager init failed", err)
   100  	}
   101  	err = s.cm.Start(s.ctx)
   102  	if err != nil {
   103  		s.T().Error("ComponentManager start failed", err)
   104  	}
   105  }
   106  
   107  func (s *metricSuite) AfterTest(suiteName, testName string) {
   108  	err := s.cm.Stop(s.ctx)
   109  	if err != nil {
   110  		s.T().Error("ComponentManager stop failed", err)
   111  	}
   112  	s.cleaner()
   113  }
   114  
   115  func (s *metricSuite) TestLedgerArtifactManager_Metrics() {
   116  	// BEWARE: this test should not be run in parallel!
   117  	mc := minimock.NewController(s.T())
   118  	defer mc.Finish()
   119  
   120  	amPulseStorageMock := testutils.NewPulseStorageMock(s.T())
   121  	amPulseStorageMock.CurrentFunc = func(p context.Context) (r *core.Pulse, r1 error) {
   122  		pulse, err := s.pulseTracker.GetLatestPulse(p)
   123  		require.NoError(s.T(), err)
   124  		return &pulse.Pulse, err
   125  	}
   126  
   127  	mb := testutils.NewMessageBusMock(mc)
   128  	mb.SendMock.Return(&reply.ID{}, nil)
   129  	cs := platformpolicy.NewPlatformCryptographyScheme()
   130  	am := NewArtifactManger()
   131  	am.DB = s.db
   132  	am.PlatformCryptographyScheme = cs
   133  	am.DefaultBus = mb
   134  	am.PulseStorage = amPulseStorageMock
   135  	am.GenesisState = s.genesisState
   136  
   137  	tmetrics := testmetrics.Start(s.ctx, s.T())
   138  	defer tmetrics.Stop()
   139  
   140  	msg := message.GenesisRequest{Name: "4K3NiGuqYGqKPnYp6XeGd2kdN4P9veL6rYcWkLKWXZCu.4FFB8zfQoGznSmzDxwv4njX1aR9ioL8GHSH17QXH2AFa"}
   141  	_, err := am.RegisterRequest(s.ctx, *am.GenesisRef(), &message.Parcel{Msg: &msg})
   142  	require.NoError(s.T(), err)
   143  
   144  	time.Sleep(1500 * time.Millisecond)
   145  
   146  	_ = am
   147  	content, err := tmetrics.FetchContent()
   148  	require.NoError(s.T(), err)
   149  
   150  	assert.Regexp(s.T(), regexp.MustCompile(`insolar_artifactmanager_latency_count{[^}]*method="RegisterRequest",result="2xx"[^}]*}`), content)
   151  	assert.Regexp(s.T(), regexp.MustCompile(`insolar_artifactmanager_calls{[^}]*method="RegisterRequest",result="2xx"[^}]*}`), content)
   152  }