github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/kafka/checkpoint_db_test.go (about) 1 // Copyright 2020 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package kafka 18 19 import ( 20 "math/rand" 21 "testing" 22 "time" 23 24 "github.com/klaytn/klaytn/storage/database" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestCheckpointDB_ReadCheckpoint(t *testing.T) { 29 rand.Seed(time.Now().UnixNano()) 30 testDB := database.NewMemoryDBManager() 31 db := NewCheckpointDB() 32 db.SetComponent(testDB) 33 34 // 1. nothing is stored 35 checkpoint, err := db.ReadCheckpoint() 36 assert.NoError(t, err) 37 assert.Equal(t, int64(0), checkpoint) 38 39 // 2. store a checkpoint 40 expected := rand.Int63() 41 assert.NoError(t, db.WriteCheckpoint(expected)) 42 43 // 3. assert the expected 44 actual, err := db.ReadCheckpoint() 45 assert.NoError(t, err) 46 assert.Equal(t, expected, actual) 47 48 // 4. overwrite checkpoint 49 expected2 := rand.Int63() 50 assert.NoError(t, db.WriteCheckpoint(expected2)) 51 52 // 5. assert the expected 2 53 actual2, err := db.ReadCheckpoint() 54 assert.NoError(t, err) 55 assert.Equal(t, expected2, actual2) 56 }