Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package waitfor

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUse(t *testing.T) {
// Create a mock module function
mockModule := func() ([]string, ResourceFactory) {
schemes := []string{"mock", "test"}
factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}
return schemes, factory
}

// Test Use function
config := Use(mockModule)

assert.Equal(t, []string{"mock", "test"}, config.Scheme)
assert.NotNil(t, config.Factory)

// Test that the factory works
testURL, _ := url.Parse("mock://example")
resource, err := config.Factory(testURL)
assert.NoError(t, err)
assert.NotNil(t, resource)
}

func TestUse_WithEmptySchemes(t *testing.T) {
// Create a module with empty schemes
mockModule := func() ([]string, ResourceFactory) {
schemes := []string{}
factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}
return schemes, factory
}

config := Use(mockModule)

assert.Empty(t, config.Scheme)
assert.NotNil(t, config.Factory)
}

func TestUse_WithNilFactory(t *testing.T) {
// Create a module with nil factory
mockModule := func() ([]string, ResourceFactory) {
schemes := []string{"nil"}
return schemes, nil
}

config := Use(mockModule)

assert.Equal(t, []string{"nil"}, config.Scheme)
assert.Nil(t, config.Factory)
}
69 changes: 69 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package waitfor

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNewOptions_Defaults(t *testing.T) {
opts := newOptions([]Option{})

assert.Equal(t, time.Duration(5)*time.Second, opts.interval)
assert.Equal(t, time.Duration(60)*time.Second, opts.maxInterval)
assert.Equal(t, uint64(5), opts.attempts)
}

func TestNewOptions_WithSetters(t *testing.T) {
setters := []Option{
WithInterval(10),
WithMaxInterval(120),
WithAttempts(15),
}

opts := newOptions(setters)

assert.Equal(t, time.Duration(10)*time.Second, opts.interval)
assert.Equal(t, time.Duration(120)*time.Second, opts.maxInterval)
assert.Equal(t, uint64(15), opts.attempts)
}

func TestWithInterval(t *testing.T) {
option := WithInterval(30)
opts := &Options{}

option(opts)

assert.Equal(t, time.Duration(30)*time.Second, opts.interval)
}

func TestWithMaxInterval(t *testing.T) {
option := WithMaxInterval(90)
opts := &Options{}

option(opts)

assert.Equal(t, time.Duration(90)*time.Second, opts.maxInterval)
}

func TestWithAttempts(t *testing.T) {
option := WithAttempts(20)
opts := &Options{}

option(opts)

assert.Equal(t, uint64(20), opts.attempts)
}

func TestCombinedOptions(t *testing.T) {
opts := newOptions([]Option{
WithInterval(2),
WithMaxInterval(30),
WithAttempts(8),
})

assert.Equal(t, time.Duration(2)*time.Second, opts.interval)
assert.Equal(t, time.Duration(30)*time.Second, opts.maxInterval)
assert.Equal(t, uint64(8), opts.attempts)
}
115 changes: 115 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package waitfor

import (
"context"
"errors"
"net/url"
"testing"

Expand Down Expand Up @@ -36,3 +37,117 @@ func TestRegistry_Register(t *testing.T) {
assert.NoError(t, err)
assert.NotNilf(t, rsc, "resource not found")
}

func TestRegistry_Register_NewScheme(t *testing.T) {
r := newRegistry([]ResourceConfig{})

factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}

err := r.Register("custom", factory)
assert.NoError(t, err)

// Verify the scheme was registered
rsc, err := r.Resolve("custom://test")
assert.NoError(t, err)
assert.NotNil(t, rsc)
}

func TestRegistry_Register_DuplicateScheme(t *testing.T) {
factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}

r := newRegistry([]ResourceConfig{
{
Scheme: []string{"existing"},
Factory: factory,
},
})

// Try to register the same scheme again
err := r.Register("existing", factory)
assert.Error(t, err)
assert.Contains(t, err.Error(), "resource is already registered with a given scheme:")
}

func TestRegistry_Register_WithWhitespace(t *testing.T) {
r := newRegistry([]ResourceConfig{})

factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}

// Register with whitespace - should be trimmed
err := r.Register(" spaced ", factory)
assert.NoError(t, err)

// Verify it was registered with trimmed name
rsc, err := r.Resolve("spaced://test")
assert.NoError(t, err)
assert.NotNil(t, rsc)
}

func TestRegistry_Resolve_InvalidURL(t *testing.T) {
r := newRegistry([]ResourceConfig{})

// Test with invalid URL
rsc, err := r.Resolve("://invalid-url")
assert.Error(t, err)
assert.Nil(t, rsc)
}

func TestRegistry_Resolve_UnknownScheme(t *testing.T) {
r := newRegistry([]ResourceConfig{})

// Test with unknown scheme
rsc, err := r.Resolve("unknown://test")
assert.Error(t, err)
assert.Nil(t, rsc)
assert.Contains(t, err.Error(), "resource with a given scheme is not found:")
}

func TestRegistry_Resolve_FactoryError(t *testing.T) {
factory := func(_ *url.URL) (Resource, error) {
return nil, errors.New("factory error")
}

r := newRegistry([]ResourceConfig{
{
Scheme: []string{"error"},
Factory: factory,
},
})

rsc, err := r.Resolve("error://test")
assert.Error(t, err)
assert.Nil(t, rsc)
assert.Contains(t, err.Error(), "factory error")
}

func TestRegistry_List(t *testing.T) {
factory := func(_ *url.URL) (Resource, error) {
return &TestResource{}, nil
}

r := newRegistry([]ResourceConfig{
{
Scheme: []string{"http", "https", "custom"},
Factory: factory,
},
})

schemes := r.List()
assert.Len(t, schemes, 3)
assert.Contains(t, schemes, "http")
assert.Contains(t, schemes, "https")
assert.Contains(t, schemes, "custom")
}

func TestRegistry_List_Empty(t *testing.T) {
r := newRegistry([]ResourceConfig{})

schemes := r.List()
assert.Empty(t, schemes)
}
Loading
Loading