|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package flushblockcache |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "strconv" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/apache/kvrocks/tests/gocase/util" |
| 28 | + "github.com/redis/go-redis/v9" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func getBlockCacheSize(rdb *redis.Client) (int64, error) { |
| 33 | + value := util.FindInfoEntry(rdb, "block_cache_usage", "rocksdb") |
| 34 | + return strconv.ParseInt(value, 10, 64) |
| 35 | +} |
| 36 | + |
| 37 | +func TestFlushBlockCache(t *testing.T) { |
| 38 | + configs := map[string]string{} |
| 39 | + srv := util.StartServer(t, configs) |
| 40 | + defer srv.Close() |
| 41 | + |
| 42 | + rdb := srv.NewClient() |
| 43 | + defer func() { |
| 44 | + require.NoError(t, rdb.Close()) |
| 45 | + }() |
| 46 | + |
| 47 | + ctx := context.Background() |
| 48 | + |
| 49 | + t.Run("flushblockcache", func(t *testing.T) { |
| 50 | + _, err := rdb.Do(ctx, "SET", "A", "KVROCKS").Result() |
| 51 | + require.NoError(t, err) |
| 52 | + _, err = rdb.Do(ctx, "FLUSHMEMTABLE").Result() |
| 53 | + require.NoError(t, err) |
| 54 | + _, err = rdb.Do(ctx, "GET", "A").Result() |
| 55 | + require.NoError(t, err) |
| 56 | + initCacheSize, err := getBlockCacheSize(rdb) |
| 57 | + require.NoError(t, err) |
| 58 | + _, err = rdb.Do(ctx, "FLUSHBLOCKCACHE").Result() |
| 59 | + require.NoError(t, err) |
| 60 | + cacheSize, err := getBlockCacheSize(rdb) |
| 61 | + require.NoError(t, err) |
| 62 | + require.Less(t, cacheSize, initCacheSize) |
| 63 | + require.Equal(t, "KVROCKS", rdb.Do(ctx, "GET", "A").Val()) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func TestFlushBlockCacheInvalid(t *testing.T) { |
| 68 | + srv := util.StartServer(t, map[string]string{}) |
| 69 | + defer srv.Close() |
| 70 | + |
| 71 | + rdb := srv.NewClient() |
| 72 | + defer func() { |
| 73 | + require.NoError(t, rdb.Close()) |
| 74 | + }() |
| 75 | + |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + t.Run("invalid arguments", func(t *testing.T) { |
| 79 | + _, err := rdb.Do(ctx, "FLUSHBLOCKCACHE", "ARG").Result() |
| 80 | + require.Contains(t, err.Error(), "wrong number of arguments") |
| 81 | + }) |
| 82 | +} |
0 commit comments