github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ts/keys_test.go (about) 1 // Copyright 2014 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package ts 12 13 import ( 14 "bytes" 15 "reflect" 16 "testing" 17 18 "github.com/cockroachdb/cockroach/pkg/keys" 19 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 20 ) 21 22 func TestDataKeys(t *testing.T) { 23 defer leaktest.AfterTest(t)() 24 testCases := []struct { 25 name string 26 source string 27 timestamp int64 28 resolution Resolution 29 expectedLen int 30 expectedPretty string 31 }{ 32 { 33 "test.metric", 34 "testsource", 35 0, 36 Resolution10s, 37 30, 38 "/System/tsd/test.metric/testsource/10s/1970-01-01T00:00:00Z", 39 }, 40 { 41 "test.no.source", 42 "", 43 1429114700000000000, 44 Resolution10s, 45 26, 46 "/System/tsd/test.no.source//10s/2015-04-15T16:00:00Z", 47 }, 48 { 49 "", 50 "", 51 -1429114700000000000, 52 Resolution10s, 53 12, 54 "/System/tsd///10s/1924-09-18T08:00:00Z", 55 }, 56 } 57 58 for i, tc := range testCases { 59 encoded := MakeDataKey(tc.name, tc.source, tc.resolution, tc.timestamp) 60 if !bytes.HasPrefix(encoded, keys.TimeseriesPrefix) { 61 t.Errorf("%d: encoded key %v did not have time series data prefix", i, encoded) 62 } 63 if a, e := len(encoded), tc.expectedLen; a != e { 64 t.Errorf("%d: encoded length %d did not match expected %d", i, a, e) 65 } 66 67 // Normalize timestamp of test case; we expect MakeDataKey to 68 // automatically truncate it to an exact multiple of the Resolution's 69 // KeyDuration 70 tc.timestamp = (tc.timestamp / tc.resolution.SlabDuration()) * tc.resolution.SlabDuration() 71 72 d := tc 73 var err error 74 d.name, d.source, d.resolution, d.timestamp, err = DecodeDataKey(encoded) 75 if err != nil { 76 t.Fatal(err) 77 } 78 if !reflect.DeepEqual(d, tc) { 79 t.Errorf("%d: decoded values %v did not match expected %v", i, d, tc) 80 } 81 if pretty := keys.PrettyPrint(nil /* valDirs */, encoded); tc.expectedPretty != pretty { 82 t.Errorf("%d: expected %s, but got %s", i, tc.expectedPretty, pretty) 83 } 84 } 85 }