|
| 1 | +// Copyright 2025 PingCAP, 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 metering |
| 16 | + |
| 17 | +import ( |
| 18 | + "maps" |
| 19 | + "strconv" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestDataEquals(t *testing.T) { |
| 26 | + cases := []struct { |
| 27 | + pair [2]Data |
| 28 | + equal bool |
| 29 | + }{ |
| 30 | + {pair: [2]Data{{taskID: 1, getRequests: 1}, {taskID: 1, getRequests: 1}}, equal: true}, |
| 31 | + {pair: [2]Data{{taskID: 1, getRequests: 1}, {taskID: 1, getRequests: 2}}, equal: false}, |
| 32 | + {pair: [2]Data{{taskID: 1, putRequests: 1}, {taskID: 1, putRequests: 1}}, equal: true}, |
| 33 | + {pair: [2]Data{{taskID: 1, putRequests: 1}, {taskID: 1, putRequests: 2}}, equal: false}, |
| 34 | + // we only compare the data fields, not taskID |
| 35 | + {pair: [2]Data{{taskID: 1, getRequests: 1}, {taskID: 2, getRequests: 1}}, equal: true}, |
| 36 | + } |
| 37 | + for i, c := range cases { |
| 38 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 39 | + require.Equal(t, c.equal, c.pair[0].equals(&c.pair[1]), "case %d failed", i) |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestDataCalMeterDataItem(t *testing.T) { |
| 45 | + currData := &Data{getRequests: 10, putRequests: 20, readBytes: 300, writeBytes: 400, taskID: 1, keyspace: "ks", taskType: "tt"} |
| 46 | + require.Nil(t, currData.calMeterDataItem(currData)) |
| 47 | + getItemFn := func(in map[string]any) map[string]any { |
| 48 | + maps.Copy(in, map[string]any{ |
| 49 | + "version": "1", |
| 50 | + "cluster_id": "ks", |
| 51 | + "source_name": category, |
| 52 | + "task_type": "tt", |
| 53 | + "task_id": int64(1), |
| 54 | + }) |
| 55 | + return in |
| 56 | + } |
| 57 | + require.EqualValues(t, getItemFn(map[string]any{ |
| 58 | + "get_requests": uint64(5), |
| 59 | + "put_requests": uint64(15), |
| 60 | + "read_bytes": uint64(200), |
| 61 | + "write_bytes": uint64(300), |
| 62 | + }), currData.calMeterDataItem(&Data{getRequests: 5, putRequests: 5, readBytes: 100, writeBytes: 100})) |
| 63 | + require.EqualValues(t, getItemFn(map[string]any{ |
| 64 | + "get_requests": uint64(5), |
| 65 | + }), currData.calMeterDataItem(&Data{getRequests: 5, putRequests: 20, readBytes: 300, writeBytes: 400})) |
| 66 | +} |
0 commit comments