adds -track-error option to add custom errors to max-host-error watchlist (#3399)

* Allow user to specify for "context deadline exceeded" errors to count toward the max host error count

* Convert flag to a string slice `--track-error`

* Minimize diff

* Add documentation for `-track-error`

* adds unit test & minor improvements

* update flag description

---------

Co-authored-by: Austin Traver <austin_traver@intuit.com>
Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>
This commit is contained in:
Austin Traver
2023-03-14 01:29:42 -07:00
committed by GitHub
parent bcb1a8811d
commit 0d90a555f6
11 changed files with 43 additions and 9 deletions

View File

@@ -10,7 +10,7 @@ import (
)
func TestCacheCheck(t *testing.T) {
cache := New(3, DefaultMaxHostsCount)
cache := New(3, DefaultMaxHostsCount, nil)
for i := 0; i < 100; i++ {
cache.MarkFailed("test", fmt.Errorf("could not resolve host"))
@@ -28,6 +28,24 @@ func TestCacheCheck(t *testing.T) {
require.Equal(t, true, value, "could not get checked value")
}
func TestTrackErrors(t *testing.T) {
cache := New(3, DefaultMaxHostsCount, []string{"custom error"})
for i := 0; i < 100; i++ {
cache.MarkFailed("custom", fmt.Errorf("got: nested: custom error"))
got := cache.Check("custom")
if i < 2 {
// till 3 the host is not flagged to skip
require.False(t, got)
} else {
// above 3 it must remain flagged to skip
require.True(t, got)
}
}
value := cache.Check("custom")
require.Equal(t, true, value, "could not get checked value")
}
func TestCacheItemDo(t *testing.T) {
var (
count int
@@ -51,7 +69,7 @@ func TestCacheItemDo(t *testing.T) {
}
func TestCacheMarkFailed(t *testing.T) {
cache := New(3, DefaultMaxHostsCount)
cache := New(3, DefaultMaxHostsCount, nil)
tests := []struct {
host string
@@ -76,7 +94,7 @@ func TestCacheMarkFailed(t *testing.T) {
}
func TestCacheMarkFailedConcurrent(t *testing.T) {
cache := New(3, DefaultMaxHostsCount)
cache := New(3, DefaultMaxHostsCount, nil)
tests := []struct {
host string