github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/events/events_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package events
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
    23  )
    24  
    25  func TestEvents(t *testing.T) {
    26  	remoteUrl := "https://dolthub.com/org/repo"
    27  
    28  	collector := NewCollector()
    29  	testEvent := NewEvent(eventsapi.ClientEventType_CLONE)
    30  
    31  	testEvent.SetAttribute(eventsapi.AttributeID_REMOTE_URL_SCHEME, remoteUrl)
    32  
    33  	counter := NewCounter(eventsapi.MetricID_METRIC_UNSPECIFIED)
    34  	counter.Inc()
    35  	testEvent.AddMetric(counter)
    36  
    37  	timer := NewTimer(eventsapi.MetricID_METRIC_UNSPECIFIED)
    38  	timer.Stop()
    39  	testEvent.AddMetric(timer)
    40  
    41  	collector.CloseEventAndAdd(testEvent)
    42  
    43  	assert.Panics(t, func() {
    44  		collector.CloseEventAndAdd(testEvent)
    45  	})
    46  
    47  	clientEvents := collector.Close()
    48  
    49  	assert.Equal(t, 1, len(clientEvents))
    50  	assert.Equal(t, 1, len(clientEvents[0].Attributes))
    51  	assert.Equal(t, 2, len(clientEvents[0].Metrics))
    52  	assert.NotNil(t, clientEvents[0].StartTime)
    53  	assert.NotNil(t, clientEvents[0].EndTime)
    54  
    55  	assert.Equal(t, eventsapi.AttributeID_REMOTE_URL_SCHEME, clientEvents[0].Attributes[0].Id)
    56  	assert.Equal(t, remoteUrl, clientEvents[0].Attributes[0].Value)
    57  	_, isCounter := clientEvents[0].Metrics[0].MetricOneof.(*eventsapi.ClientEventMetric_Count)
    58  	assert.True(t, isCounter)
    59  	_, isTimer := clientEvents[0].Metrics[1].MetricOneof.(*eventsapi.ClientEventMetric_Duration)
    60  	assert.True(t, isTimer)
    61  }