Skip to content

Commit dc65385

Browse files
committed
Fix, timeout of 0 was not a timeout of "infinity".
1 parent 8bdce7a commit dc65385

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

diffmatchpatch/dmp.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff
256256
// DiffMainRunes finds the differences between two rune sequences.
257257
func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff {
258258
var deadline time.Time
259-
if dmp.DiffTimeout <= 0 {
260-
deadline = time.Now().Add(24 * 365 * time.Hour)
261-
} else {
259+
if dmp.DiffTimeout > 0 {
262260
deadline = time.Now().Add(dmp.DiffTimeout)
263261
}
264262
return dmp.diffMainRunes(text1, text2, checklines, deadline)
@@ -456,7 +454,7 @@ func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time)
456454
k2end := 0
457455
for d := 0; d < maxD; d++ {
458456
// Bail out if deadline is reached.
459-
if time.Now().After(deadline) {
457+
if !deadline.IsZero() && time.Now().After(deadline) {
460458
break
461459
}
462460

diffmatchpatch/dmp_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,18 +907,21 @@ func Test_diffBisect(t *testing.T) {
907907
// Since the resulting diff hasn't been normalized, it would be ok if
908908
// the insertion and deletion pairs are swapped.
909909
// If the order changes, tweak this test as required.
910-
diffs := []Diff{
910+
correctDiffs := []Diff{
911911
Diff{DiffDelete, "c"},
912912
Diff{DiffInsert, "m"},
913913
Diff{DiffEqual, "a"},
914914
Diff{DiffDelete, "t"},
915915
Diff{DiffInsert, "p"}}
916916

917-
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Date(9999, time.December, 31, 23, 59, 59, 59, time.UTC)))
917+
assertDiffEqual(t, correctDiffs, dmp.DiffBisect(a, b, time.Date(9999, time.December, 31, 23, 59, 59, 59, time.UTC)))
918918

919919
// Timeout.
920-
diffs = []Diff{Diff{DiffDelete, "cat"}, Diff{DiffInsert, "map"}}
921-
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Date(0001, time.January, 01, 00, 00, 00, 00, time.UTC)))
920+
diffs := []Diff{Diff{DiffDelete, "cat"}, Diff{DiffInsert, "map"}}
921+
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Now().Add(time.Nanosecond)))
922+
923+
// Negative deadlines count as having infinite time.
924+
assertDiffEqual(t, correctDiffs, dmp.DiffBisect(a, b, time.Date(0001, time.January, 01, 00, 00, 00, 00, time.UTC)))
922925
}
923926

924927
func Test_diffMain(t *testing.T) {

0 commit comments

Comments
 (0)