github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/config/bytesize_test.go (about) 1 // Copyright 2020 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package config_test 15 16 import ( 17 "encoding/json" 18 "strings" 19 20 "github.com/BurntSushi/toml" 21 . "github.com/pingcap/check" 22 23 "github.com/pingcap/br/pkg/lightning/config" 24 ) 25 26 type byteSizeTestSuite struct{} 27 28 var _ = Suite(&byteSizeTestSuite{}) 29 30 func (s *byteSizeTestSuite) TestByteSizeTOMLDecode(c *C) { 31 testCases := []struct { 32 input string 33 output config.ByteSize 34 err string 35 }{ 36 { 37 input: "x = 10000", 38 output: 10000, 39 }, 40 { 41 input: "x = 107_374_182_400", 42 output: 107_374_182_400, 43 }, 44 { 45 input: "x = '10k'", 46 output: 10 * 1024, 47 }, 48 { 49 input: "x = '10PiB'", 50 output: 10 * 1024 * 1024 * 1024 * 1024 * 1024, 51 }, 52 { 53 input: "x = '10 KB'", 54 output: 10 * 1024, 55 }, 56 { 57 input: "x = '32768'", 58 output: 32768, 59 }, 60 { 61 input: "x = -1", 62 err: "invalid size: '-1'", 63 }, 64 { 65 input: "x = 'invalid value'", 66 err: "invalid size: 'invalid value'", 67 }, 68 { 69 input: "x = true", 70 err: "invalid size: 'true'", 71 }, 72 { 73 input: "x = 256.0", 74 output: 256, 75 }, 76 { 77 input: "x = 256.9", 78 output: 256, 79 }, 80 { 81 input: "x = 10e+9", 82 output: 10_000_000_000, 83 }, 84 { 85 input: "x = '2.5MB'", 86 output: 5 * 512 * 1024, 87 }, 88 { 89 input: "x = 2020-01-01T00:00:00Z", 90 err: "invalid size: '2020-01-01T00:00:00Z'", 91 }, 92 { 93 input: "x = ['100000']", 94 err: "toml: cannot load TOML value.*", 95 }, 96 { 97 input: "x = { size = '100000' }", 98 err: "toml: cannot load TOML value.*", 99 }, 100 } 101 102 for _, tc := range testCases { 103 comment := Commentf("input: `%s`", tc.input) 104 var output struct{ X config.ByteSize } 105 err := toml.Unmarshal([]byte(tc.input), &output) 106 if tc.err != "" { 107 c.Assert(err, ErrorMatches, tc.err, comment) 108 } else { 109 c.Assert(err, IsNil, comment) 110 c.Assert(output.X, Equals, tc.output, comment) 111 } 112 } 113 } 114 115 func (s *byteSizeTestSuite) TestByteSizeTOMLAndJSONEncode(c *C) { 116 var input struct { 117 X config.ByteSize `toml:"x" json:"x"` 118 } 119 input.X = 1048576 120 121 var output strings.Builder 122 err := toml.NewEncoder(&output).Encode(input) 123 c.Assert(err, IsNil) 124 c.Assert(output.String(), Equals, "x = 1048576\n") 125 126 js, err := json.Marshal(input) 127 c.Assert(err, IsNil) 128 c.Assert(string(js), Equals, `{"x":1048576}`) 129 }