-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chores: fix CI lint complains and add simple tests on the way
- Loading branch information
Showing
13 changed files
with
169 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfig_populateEmpty(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
input Config | ||
other *Config | ||
want *Config | ||
}{ | ||
{ | ||
input: Config{}, | ||
other: &Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
want: &Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
}, | ||
{ | ||
input: Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
other: &Config{}, | ||
want: &Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
}, | ||
{ | ||
input: Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
other: &Config{ | ||
Skip: ptr([]string{"world"}), | ||
}, | ||
want: &Config{ | ||
Skip: ptr([]string{"hello"}), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
originput, err := json.Marshal(tt.input) | ||
require.NoError(t, err) | ||
|
||
origother, err := json.Marshal(tt.other) | ||
require.NoError(t, err) | ||
|
||
got := tt.input.populateEmpty(tt.other) | ||
require.Equal(t, tt.want, got) | ||
|
||
afterinput, err := json.Marshal(tt.input) | ||
require.NoError(t, err) | ||
require.Equal(t, originput, afterinput, "input shouldn't be changed") | ||
|
||
afterother, err := json.Marshal(tt.other) | ||
require.NoError(t, err) | ||
require.Equal(t, origother, afterother, "other shouldn't be changed") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRetrySuccess(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
now := time.Date(2024, time.October, 25, 13, 40, 37, 0, time.UTC) | ||
start := now | ||
r := rand.New(rand.NewSource(now.UnixNano())) | ||
tch := make(chan time.Time, 10) | ||
tch <- now | ||
|
||
called := 0 | ||
err := Do(ctx, | ||
func(ctx context.Context) (bool, error) { | ||
called++ | ||
return false, nil | ||
}, | ||
UseRand(r), | ||
useSleepFn(func(d time.Duration) <-chan time.Time { | ||
t.Logf("sleeping for %v", d) | ||
now = now.Add(d) | ||
go func() { | ||
select { | ||
case tch <- now: | ||
case <-ctx.Done(): | ||
panic("so sloooow") | ||
} | ||
}() | ||
return tch | ||
}), | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, called) | ||
require.Equal(t, start, now) | ||
} | ||
|
||
func TestRetryFailOnce(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
now := time.Date(2024, time.October, 25, 13, 40, 37, 0, time.UTC) | ||
start := now | ||
r := rand.New(rand.NewSource(now.UnixNano())) | ||
tch := make(chan time.Time, 1) | ||
tch <- now | ||
|
||
called := 0 | ||
err := Do(ctx, | ||
func(ctx context.Context) (bool, error) { | ||
called++ | ||
return called != 2, nil | ||
}, | ||
UseRand(r), | ||
useSleepFn(func(d time.Duration) <-chan time.Time { | ||
t.Logf("sleeping for %v", d) | ||
now = now.Add(d) | ||
go func() { | ||
select { | ||
case tch <- now: | ||
case <-ctx.Done(): | ||
panic("so sloooow") | ||
} | ||
}() | ||
return tch | ||
}), | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, called) | ||
require.Equal(t, start.Add(61015267), now) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters