[GH-ISSUE #30] Duplicate lines in hostsfile after Flush() #12

Closed
opened 2026-03-02 05:08:06 +03:00 by kerem · 1 comment
Owner

Originally created by @cfergeau on GitHub (Jan 4, 2022).
Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/30

commit 946b651a0 introduced a regression, Flush() duplicates some hostfiles lines as shown by this test case:

commit 88dad12e0131601416e2f488856f33c3d3ea80f3
Author: Christophe Fergeau <cfergeau@redhat.com>
Date:   Tue Jan 4 11:42:25 2022 +0100

    test: Add TestHosts_Flush() test case
    
    This tests Hosts.Flush(), which currently seems to be duplicating
    some hosts file entries.
    
    This is a regression which was introduced in commit 946b651a0.

diff --git a/hosts_test.go b/hosts_test.go
index 043f262..8d4482b 100644
--- a/hosts_test.go
+++ b/hosts_test.go
@@ -241,7 +241,7 @@ func BenchmarkHosts_Flush500k(b *testing.B) {
 	benchmarkHosts_Flush(50, b)
 }
 
-// benchmarks flushing a hostsfile and confirms the hashmap lookup for ips/hosts is thread save via mutex + locking
+// benchmarks flushing a hostsfile and confirms the hashmap lookup for ips/hosts is thread-safe via mutex + locking
 func benchmarkHosts_Flush(c int, b *testing.B) {
 	_, err := os.Create("hostsfile")
 	assert.Nil(b, err)
@@ -263,3 +263,17 @@ func benchmarkHosts_Flush(c int, b *testing.B) {
 	assert.Nil(b, hosts.Flush())
 	assert.Nil(b, os.Remove("hostsfile"))
 }
+
+func TestHosts_Flush(t *testing.T) {
+	_, err := os.Create("hostsfile")
+	assert.Nil(t, err)
+	hosts, err := NewCustomHosts("hostsfile")
+	assert.Nil(t, err)
+	defer assert.Nil(t, os.Remove("hostsfile"))
+	assert.Nil(t, hosts.Add("127.0.0.2", "host1"))
+	assert.Equal(t, 1, len(hosts.Lines))
+	assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
+	assert.Nil(t, hosts.Flush())
+	assert.Equal(t, 1, len(hosts.Lines))
+	assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
+}
Originally created by @cfergeau on GitHub (Jan 4, 2022). Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/30 commit 946b651a0 introduced a regression, `Flush()` duplicates some hostfiles lines as shown by this test case: ``` commit 88dad12e0131601416e2f488856f33c3d3ea80f3 Author: Christophe Fergeau <cfergeau@redhat.com> Date: Tue Jan 4 11:42:25 2022 +0100 test: Add TestHosts_Flush() test case This tests Hosts.Flush(), which currently seems to be duplicating some hosts file entries. This is a regression which was introduced in commit 946b651a0. diff --git a/hosts_test.go b/hosts_test.go index 043f262..8d4482b 100644 --- a/hosts_test.go +++ b/hosts_test.go @@ -241,7 +241,7 @@ func BenchmarkHosts_Flush500k(b *testing.B) { benchmarkHosts_Flush(50, b) } -// benchmarks flushing a hostsfile and confirms the hashmap lookup for ips/hosts is thread save via mutex + locking +// benchmarks flushing a hostsfile and confirms the hashmap lookup for ips/hosts is thread-safe via mutex + locking func benchmarkHosts_Flush(c int, b *testing.B) { _, err := os.Create("hostsfile") assert.Nil(b, err) @@ -263,3 +263,17 @@ func benchmarkHosts_Flush(c int, b *testing.B) { assert.Nil(b, hosts.Flush()) assert.Nil(b, os.Remove("hostsfile")) } + +func TestHosts_Flush(t *testing.T) { + _, err := os.Create("hostsfile") + assert.Nil(t, err) + hosts, err := NewCustomHosts("hostsfile") + assert.Nil(t, err) + defer assert.Nil(t, os.Remove("hostsfile")) + assert.Nil(t, hosts.Add("127.0.0.2", "host1")) + assert.Equal(t, 1, len(hosts.Lines)) + assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw) + assert.Nil(t, hosts.Flush()) + assert.Equal(t, 1, len(hosts.Lines)) + assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw) +} ```
kerem closed this issue 2026-03-02 05:08:06 +03:00
Author
Owner

@luthermonson commented on GitHub (Jan 5, 2022):

thanks for the failing test. let me try and figure it out

<!-- gh-comment-id:1006129765 --> @luthermonson commented on GitHub (Jan 5, 2022): thanks for the failing test. let me try and figure it out
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hostsfile#12
No description provided.