Przeglądaj źródła

fix analogous issue for history

History couldn't be enabled by rehash if autoresize-window was nonzero.
tags/v2.0.0-rc1
Shivaram Lingamneni 4 lat temu
rodzic
commit
bd6c2117e8
2 zmienionych plików z 52 dodań i 11 usunięć
  1. 23
    11
      irc/history/history.go
  2. 29
    0
      irc/history/history_test.go

+ 23
- 11
irc/history/history.go Wyświetl plik

@@ -101,14 +101,7 @@ func NewHistoryBuffer(size int, window time.Duration) (result *Buffer) {
101 101
 }
102 102
 
103 103
 func (hist *Buffer) Initialize(size int, window time.Duration) {
104
-	initialSize := size
105
-	if window != 0 {
106
-		initialSize = initialAutoSize
107
-		if size < initialSize {
108
-			initialSize = size // min(initialAutoSize, size)
109
-		}
110
-	}
111
-	hist.buffer = make([]Item, initialSize)
104
+	hist.buffer = make([]Item, hist.initialSize(size, window))
112 105
 	hist.start = -1
113 106
 	hist.end = -1
114 107
 	hist.window = window
@@ -118,6 +111,18 @@ func (hist *Buffer) Initialize(size int, window time.Duration) {
118 111
 	hist.setEnabled(size)
119 112
 }
120 113
 
114
+// compute the initial size for the buffer, taking into account autoresize
115
+func (hist *Buffer) initialSize(size int, window time.Duration) (result int) {
116
+	result = size
117
+	if window != 0 {
118
+		result = initialAutoSize
119
+		if size < result {
120
+			result = size // min(initialAutoSize, size)
121
+		}
122
+	}
123
+	return
124
+}
125
+
121 126
 func (hist *Buffer) setEnabled(size int) {
122 127
 	var enabled uint32
123 128
 	if size != 0 {
@@ -339,12 +344,19 @@ func (list *Buffer) Resize(maximumSize int, window time.Duration) {
339 344
 	list.maximumSize = maximumSize
340 345
 	list.window = window
341 346
 
342
-	// if we're not autoresizing, we need to resize now;
343
-	// if we are autoresizing, we may need to shrink the buffer down to maximumSize,
344
-	// but we don't need to grow it now (we can just grow it on the next Add)
347
+	// three cases where we need to preemptively resize:
348
+	// (1) we are not autoresizing
349
+	// (2) the buffer is currently larger than maximumSize and needs to be shrunk
350
+	// (3) the buffer is currently smaller than the recommended initial size
351
+	//     (including the case where it is currently disabled and needs to be enabled)
345 352
 	// TODO make it possible to shrink the buffer so that it only contains `window`
346 353
 	if window == 0 || maximumSize < len(list.buffer) {
347 354
 		list.resize(maximumSize)
355
+	} else {
356
+		initialSize := list.initialSize(maximumSize, window)
357
+		if len(list.buffer) < initialSize {
358
+			list.resize(initialSize)
359
+		}
348 360
 	}
349 361
 }
350 362
 

+ 29
- 0
irc/history/history_test.go Wyświetl plik

@@ -213,6 +213,35 @@ func TestAutoresize(t *testing.T) {
213 213
 	assertEqual(atoi(items[len(items)-1].Nick), 271, t)
214 214
 }
215 215
 
216
+// regression test for #702
217
+func TestEnabledByResize(t *testing.T) {
218
+	now := easyParse("2006-01-01 00:00:00Z")
219
+	// empty/disabled autoresizing buffer
220
+	buf := NewHistoryBuffer(0, time.Hour)
221
+	// enable the buffer as during a rehash
222
+	buf.Resize(128, time.Hour)
223
+	// add an item and test that it is stored and retrievable
224
+	buf.Add(autoItem(0, now))
225
+	items := buf.Latest(0)
226
+	assertEqual(len(items), 1, t)
227
+	assertEqual(atoi(items[0].Nick), 0, t)
228
+}
229
+
230
+func TestDisabledByResize(t *testing.T) {
231
+	now := easyParse("2006-01-01 00:00:00Z")
232
+	// enabled autoresizing buffer
233
+	buf := NewHistoryBuffer(128, time.Hour)
234
+	buf.Add(autoItem(0, now))
235
+	items := buf.Latest(0)
236
+	assertEqual(len(items), 1, t)
237
+	assertEqual(atoi(items[0].Nick), 0, t)
238
+
239
+	// disable as during a rehash, confirm that nothing can be retrieved
240
+	buf.Resize(0, time.Hour)
241
+	items = buf.Latest(0)
242
+	assertEqual(len(items), 0, t)
243
+}
244
+
216 245
 func TestRoundUp(t *testing.T) {
217 246
 	assertEqual(roundUpToPowerOfTwo(2), 2, t)
218 247
 	assertEqual(roundUpToPowerOfTwo(3), 4, t)

Ładowanie…
Anuluj
Zapisz