-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This kind of source can be useful in tests. This commit adds example test for HTTP target with in memory source in `cmd/inmemory_tests`.
- Loading branch information
Showing
5 changed files
with
361 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/snowplow/snowbridge/cmd" | ||
"github.com/snowplow/snowbridge/cmd/cli" | ||
"github.com/snowplow/snowbridge/config" | ||
"github.com/snowplow/snowbridge/pkg/source/inmemory" | ||
"github.com/snowplow/snowbridge/pkg/transform/transformconfig" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHTTPTarget(t *testing.T) { | ||
inputChannel := make(chan []string) | ||
outputBuffer := []string{} | ||
wg := sync.WaitGroup{} | ||
server := createHTTPServer(&outputBuffer, &wg) | ||
t.Setenv("SNOWBRIDGE_CONFIG_FILE", "./test_config.hcl") | ||
t.Setenv("SERVER_URL", server.URL) | ||
defer server.Close() | ||
|
||
runTest(inputChannel) | ||
|
||
wg.Add(3) // 3 batches -> 3 output HTTP requests | ||
inputChannel <- []string{`{"field": "mes1"}`, `{"field": "mes2"}`} | ||
time.Sleep(2 * time.Second) | ||
inputChannel <- []string{`{"field": "mes3"}`} | ||
time.Sleep(2 * time.Second) | ||
inputChannel <- []string{`{"field": "mes4"}`, `{"field": "mes5"}`} | ||
|
||
if ok := withTimeout(time.Second, &wg); !ok { | ||
assert.Fail(t, "Timeout! No expected data within configured time") | ||
} | ||
|
||
expectedOutput := []string{ | ||
`[{"field":"mes1"},{"field":"mes2"}]`, | ||
`[{"field":"mes3"}]`, | ||
`[{"field":"mes4"},{"field":"mes5"}]`, | ||
} | ||
|
||
assert.Equal(t, expectedOutput, outputBuffer) | ||
} | ||
|
||
func runTest(input chan []string) { | ||
sourceConfigPairs := []config.ConfigurationPair{inmemory.ConfigPair(input)} | ||
|
||
config, _, err := cmd.Init() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go func() { | ||
err := cli.RunApp(config, sourceConfigPairs, transformconfig.SupportedTransformations) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
} | ||
|
||
func createHTTPServer(output *[]string, wg *sync.WaitGroup) *httptest.Server{ | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
defer req.Body.Close() | ||
data, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
*output = append(*output, string(data)) | ||
defer wg.Done() | ||
})) | ||
} | ||
|
||
func withTimeout(timeout time.Duration, wg *sync.WaitGroup) bool { | ||
c := make(chan struct{}) | ||
go func() { | ||
defer close(c) | ||
wg.Wait() | ||
}() | ||
select { | ||
case <-c: | ||
return true | ||
case <-time.After(timeout): | ||
return false | ||
} | ||
} |
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,13 @@ | ||
target { | ||
use "http" { | ||
url = env.SERVER_URL | ||
} | ||
} | ||
|
||
source { | ||
use "inMemory" {} | ||
} | ||
|
||
license { | ||
accept = true | ||
} |
Oops, something went wrong.