|
20 | 20 |
|
21 | 21 | import org.elasticsearch.common.collect.Tuple;
|
22 | 22 | import org.elasticsearch.common.settings.Setting.Property;
|
| 23 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
23 | 24 | import org.elasticsearch.common.unit.ByteSizeValue;
|
24 | 25 | import org.elasticsearch.common.unit.TimeValue;
|
25 | 26 | import org.elasticsearch.monitor.jvm.JvmInfo;
|
@@ -52,35 +53,61 @@ public void testGet() {
|
52 | 53 | assertTrue(booleanSetting.get(Settings.builder().put("foo.bar", true).build()));
|
53 | 54 | }
|
54 | 55 |
|
55 |
| - public void testByteSize() { |
56 |
| - Setting<ByteSizeValue> byteSizeValueSetting = |
57 |
| - Setting.byteSizeSetting("a.byte.size", new ByteSizeValue(1024), Property.Dynamic, Property.NodeScope); |
| 56 | + public void testByteSizeSetting() { |
| 57 | + final Setting<ByteSizeValue> byteSizeValueSetting = |
| 58 | + Setting.byteSizeSetting("a.byte.size", new ByteSizeValue(1024), Property.Dynamic, Property.NodeScope); |
58 | 59 | assertFalse(byteSizeValueSetting.isGroupSetting());
|
59 |
| - ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY); |
60 |
| - assertEquals(byteSizeValue.getBytes(), 1024); |
61 |
| - |
62 |
| - byteSizeValueSetting = Setting.byteSizeSetting("a.byte.size", s -> "2048b", Property.Dynamic, Property.NodeScope); |
63 |
| - byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY); |
64 |
| - assertEquals(byteSizeValue.getBytes(), 2048); |
65 |
| - |
66 |
| - |
| 60 | + final ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY); |
| 61 | + assertThat(byteSizeValue.getBytes(), equalTo(1024L)); |
| 62 | + } |
| 63 | + |
| 64 | + public void testByteSizeSettingMinValue() { |
| 65 | + final Setting<ByteSizeValue> byteSizeValueSetting = |
| 66 | + Setting.byteSizeSetting( |
| 67 | + "a.byte.size", |
| 68 | + new ByteSizeValue(100, ByteSizeUnit.MB), |
| 69 | + new ByteSizeValue(20_000_000, ByteSizeUnit.BYTES), |
| 70 | + new ByteSizeValue(Integer.MAX_VALUE, ByteSizeUnit.BYTES)); |
| 71 | + final long value = 20_000_000 - randomIntBetween(1, 1024); |
| 72 | + final Settings settings = Settings.builder().put("a.byte.size", value + "b").build(); |
| 73 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> byteSizeValueSetting.get(settings)); |
| 74 | + final String expectedMessage = "failed to parse value [" + value + "b] for setting [a.byte.size], must be >= [20000000b]"; |
| 75 | + assertThat(e, hasToString(containsString(expectedMessage))); |
| 76 | + } |
| 77 | + |
| 78 | + public void testByteSizeSettingMaxValue() { |
| 79 | + final Setting<ByteSizeValue> byteSizeValueSetting = |
| 80 | + Setting.byteSizeSetting( |
| 81 | + "a.byte.size", |
| 82 | + new ByteSizeValue(100, ByteSizeUnit.MB), |
| 83 | + new ByteSizeValue(16, ByteSizeUnit.MB), |
| 84 | + new ByteSizeValue(Integer.MAX_VALUE, ByteSizeUnit.BYTES)); |
| 85 | + final long value = (1L << 31) - 1 + randomIntBetween(1, 1024); |
| 86 | + final Settings settings = Settings.builder().put("a.byte.size", value + "b").build(); |
| 87 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> byteSizeValueSetting.get(settings)); |
| 88 | + final String expectedMessage = "failed to parse value [" + value + "b] for setting [a.byte.size], must be <= [2147483647b]"; |
| 89 | + assertThat(e, hasToString(containsString(expectedMessage))); |
| 90 | + } |
| 91 | + |
| 92 | + public void testByteSizeSettingValidation() { |
| 93 | + final Setting<ByteSizeValue> byteSizeValueSetting = |
| 94 | + Setting.byteSizeSetting("a.byte.size", s -> "2048b", Property.Dynamic, Property.NodeScope); |
| 95 | + final ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY); |
| 96 | + assertThat(byteSizeValue.getBytes(), equalTo(2048L)); |
67 | 97 | AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
|
68 | 98 | ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = byteSizeValueSetting.newUpdater(value::set, logger);
|
69 |
| - try { |
70 |
| - settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY); |
71 |
| - fail("no unit"); |
72 |
| - } catch (IllegalArgumentException ex) { |
73 |
| - assertThat(ex, hasToString(containsString("illegal value can't update [a.byte.size] from [2048b] to [12]"))); |
74 |
| - assertNotNull(ex.getCause()); |
75 |
| - assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class)); |
76 |
| - final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause(); |
77 |
| - final String expected = |
78 |
| - "failed to parse setting [a.byte.size] with value [12] as a size in bytes: unit is missing or unrecognized"; |
79 |
| - assertThat(cause, hasToString(containsString(expected))); |
80 |
| - } |
81 | 99 |
|
| 100 | + final IllegalArgumentException e = expectThrows( |
| 101 | + IllegalArgumentException.class, |
| 102 | + () -> settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY)); |
| 103 | + assertThat(e, hasToString(containsString("illegal value can't update [a.byte.size] from [2048b] to [12]"))); |
| 104 | + assertNotNull(e.getCause()); |
| 105 | + assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); |
| 106 | + final IllegalArgumentException cause = (IllegalArgumentException) e.getCause(); |
| 107 | + final String expected = "failed to parse setting [a.byte.size] with value [12] as a size in bytes: unit is missing or unrecognized"; |
| 108 | + assertThat(cause, hasToString(containsString(expected))); |
82 | 109 | assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "12b").build(), Settings.EMPTY));
|
83 |
| - assertEquals(new ByteSizeValue(12), value.get()); |
| 110 | + assertThat(value.get(), equalTo(new ByteSizeValue(12))); |
84 | 111 | }
|
85 | 112 |
|
86 | 113 | public void testMemorySize() {
|
|
0 commit comments