github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/events/event_flush_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  	"context"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"google.golang.org/grpc"
    24  
    25  	eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
    26  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    27  	testLib "github.com/dolthub/dolt/go/libraries/utils/test"
    28  )
    29  
    30  var (
    31  	testVersion = "1.0.0"
    32  	homeDir     = "/home/"
    33  	dPath       = ".dolt"
    34  	evtPath     = eventsDir
    35  	doltTestDir = filepath.Join(homeDir, dPath)
    36  	tempEvtsDir = filepath.Join(doltTestDir, evtPath)
    37  )
    38  
    39  type TestClient struct {
    40  	CES []*eventsapi.ClientEvent
    41  }
    42  
    43  func (tc *TestClient) LogEvents(ctx context.Context, in *eventsapi.LogEventsRequest, opts ...grpc.CallOption) (*eventsapi.LogEventsResponse, error) {
    44  	for _, evt := range in.Events {
    45  		tc.CES = append(tc.CES, evt)
    46  	}
    47  	return &eventsapi.LogEventsResponse{}, nil
    48  }
    49  
    50  func NewTestClient() *TestClient {
    51  	return &TestClient{}
    52  }
    53  
    54  type flushTester struct {
    55  	Client  *TestClient
    56  	Fbp     *FileBackedProc
    57  	Flusher *GrpcEventFlusher
    58  }
    59  
    60  func createFlushTester(fs filesys.Filesys, hdir string, ddir string) *flushTester {
    61  	client := NewTestClient()
    62  
    63  	sn := NewSequentialNamer()
    64  
    65  	fbp := NewFileBackedProc(fs, hdir, ddir, sn.Name, sn.Check)
    66  
    67  	gef := &GrpcEventFlusher{em: &GrpcEmitter{client}, fbp: fbp}
    68  
    69  	return &flushTester{Client: client, Fbp: fbp, Flusher: gef}
    70  }
    71  
    72  func TestEventFlushing(t *testing.T) {
    73  	tests := []struct {
    74  		name      string
    75  		numEvents int
    76  	}{
    77  		{
    78  			name:      "Flush 0 events",
    79  			numEvents: 0,
    80  		},
    81  		{
    82  			name:      "Flush 100 events",
    83  			numEvents: 100,
    84  		},
    85  		{
    86  			name:      "Flush 1000 events",
    87  			numEvents: 1000,
    88  		},
    89  	}
    90  
    91  	filesystems := []string{"inMemFS", "local"}
    92  
    93  	for _, fsName := range filesystems {
    94  		for _, test := range tests {
    95  
    96  			t.Run(test.name, func(t *testing.T) {
    97  				ctx := context.Background()
    98  
    99  				var ft *flushTester
   100  
   101  				if fsName == "inMemFS" {
   102  					fs := filesys.NewInMemFS([]string{tempEvtsDir}, nil, tempEvtsDir)
   103  
   104  					ft = createFlushTester(fs, homeDir, doltTestDir)
   105  				} else {
   106  					fs := filesys.LocalFS
   107  
   108  					path := filepath.Join(dPath, evtPath)
   109  					dDir := testLib.TestDir(path)
   110  
   111  					ft = createFlushTester(fs, "", dDir)
   112  				}
   113  
   114  				ces := make([]*eventsapi.ClientEvent, 0)
   115  
   116  				for i := 0; i < test.numEvents; i++ {
   117  					ce := &eventsapi.ClientEvent{}
   118  					ces = append(ces, ce)
   119  				}
   120  
   121  				assert.Equal(t, len(ces), test.numEvents)
   122  
   123  				err := ft.Fbp.WriteEvents(testVersion, ces)
   124  				assert.Equal(t, err, nil)
   125  
   126  				err = ft.Flusher.Flush(ctx)
   127  
   128  				assert.Equal(t, err, nil)
   129  				assert.Equal(t, len(ft.Client.CES), len(ces))
   130  			})
   131  		}
   132  	}
   133  }