github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/testclient_test.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 logservice 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/lni/vfs" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestTestClient(t *testing.T) { 29 fs := vfs.NewStrictMem() 30 service, ccfg, err := NewTestService(fs) 31 require.NoError(t, err) 32 defer service.Close() 33 34 // you need to decision what timeout value to use 35 // you also need to retry requests on timeout 36 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 37 defer cancel() 38 39 // get two clients, you can get many clients to be used concurrently 40 // but each client itself is not goroutine safe 41 client1, err := NewClient(ctx, ccfg) 42 require.NoError(t, err) 43 defer client1.Close() 44 client2, err := NewClient(ctx, ccfg) 45 require.NoError(t, err) 46 defer client2.Close() 47 48 // Don't use the Data field unless you know what you are doing 49 rec1 := client1.GetLogRecord(5) 50 rec2 := client2.GetLogRecord(5) 51 copy(rec1.Payload(), []byte("hello")) 52 copy(rec2.Payload(), []byte("world")) 53 54 // append the records 55 lsn1, err := client1.Append(ctx, rec1) 56 require.NoError(t, err) 57 lsn2, err := client2.Append(ctx, rec2) 58 require.NoError(t, err) 59 assert.Equal(t, lsn1+1, lsn2) 60 61 // read them back and check whether the returned data is expected 62 recs, lsn, err := client1.Read(ctx, lsn1, 1024) 63 require.NoError(t, err) 64 assert.Equal(t, lsn1, lsn) 65 assert.Equal(t, 2, len(recs)) 66 assert.Equal(t, rec1.Payload(), recs[0].Payload()) 67 assert.Equal(t, rec2.Payload(), recs[1].Payload()) 68 }