-
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.
- Loading branch information
Showing
4 changed files
with
277 additions
and
73 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,116 @@ | ||
/** | ||
* Copyright (c) 2020-present Snowplow Analytics Ltd. | ||
* All rights reserved. | ||
* | ||
* This software is made available by Snowplow Analytics, Ltd., | ||
* under the terms of the Snowplow Limited Use License Agreement, Version 1.0 | ||
* located at https://docs.snowplow.io/limited-use-license-1.0 | ||
* BY INSTALLING, DOWNLOADING, ACCESSING, USING OR DISTRIBUTING ANY PORTION | ||
* OF THE SOFTWARE, YOU AGREE TO THE TERMS OF SUCH LICENSE AGREEMENT. | ||
*/ | ||
|
||
package inmemory | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/snowplow/snowbridge/config" | ||
"github.com/snowplow/snowbridge/pkg/models" | ||
"github.com/snowplow/snowbridge/pkg/source/sourceiface" | ||
"github.com/twinj/uuid" | ||
) | ||
|
||
type Configuration struct{} | ||
|
||
type inMemorySource struct { | ||
messages chan []string | ||
log *log.Entry | ||
exitSignal chan struct{} | ||
} | ||
|
||
func configfunction(messages chan []string) func(c *Configuration) (sourceiface.Source, error) { | ||
return func(c *Configuration) (sourceiface.Source, error) { | ||
return newInMemorySource(messages) | ||
} | ||
} | ||
|
||
type adapter func(i interface{}) (interface{}, error) | ||
|
||
func (f adapter) Create(i interface{}) (interface{}, error) { | ||
return f(i) | ||
} | ||
|
||
func (f adapter) ProvideDefault() (interface{}, error) { | ||
cfg := &Configuration{} | ||
|
||
return cfg, nil | ||
} | ||
|
||
func adapterGenerator(f func(c *Configuration) (sourceiface.Source, error)) adapter { | ||
return func(i interface{}) (interface{}, error) { | ||
cfg, ok := i.(*Configuration) | ||
if !ok { | ||
return nil, errors.New("invalid input") | ||
} | ||
|
||
return f(cfg) | ||
} | ||
} | ||
|
||
func ConfigPair(messages chan []string) config.ConfigurationPair { | ||
return config.ConfigurationPair{ | ||
Name: "inMemory", | ||
Handle: adapterGenerator(configfunction(messages)), | ||
} | ||
} | ||
|
||
func newInMemorySource(messages chan []string) (*inMemorySource, error) { | ||
return &inMemorySource{ | ||
log: log.WithFields(log.Fields{"source": "in_memory"}), | ||
messages: messages, | ||
exitSignal: make(chan struct{}), | ||
}, nil | ||
} | ||
|
||
func (ss *inMemorySource) Read(sf *sourceiface.SourceFunctions) error { | ||
ss.log.Infof("Reading messages from in memory buffer") | ||
|
||
processing: | ||
for { | ||
select { | ||
case <-ss.exitSignal: | ||
break processing | ||
case msgs := <-ss.messages: | ||
timeNow := time.Now().UTC() | ||
var mods []*models.Message | ||
for _, m := range msgs { | ||
mod := models.Message{ | ||
Data: []byte(m), | ||
PartitionKey: uuid.NewV4().String(), | ||
TimeCreated: timeNow, | ||
TimePulled: timeNow, | ||
} | ||
mods = append(mods, &mod) | ||
} | ||
|
||
err := sf.WriteToTarget(mods) | ||
if err != nil { | ||
ss.log.WithFields(log.Fields{"error": err}).Error(err) | ||
} | ||
} | ||
} | ||
|
||
ss.log.Infof("Done with processing") | ||
return nil | ||
} | ||
|
||
func (ss *inMemorySource) Stop() { | ||
ss.log.Warn("Stopping in memory source") | ||
ss.exitSignal <- struct{}{} | ||
} | ||
|
||
func (ss *inMemorySource) GetID() string { | ||
return "inMemory" | ||
} |
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,70 @@ | ||
package experimental | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"testing" | ||
"time" | ||
"net/http" | ||
|
||
"github.com/snowplow/snowbridge/cmd" | ||
"github.com/snowplow/snowbridge/cmd/cli" | ||
"github.com/snowplow/snowbridge/config" | ||
inmemorysource "github.com/snowplow/snowbridge/pkg/source/inmemory" | ||
"github.com/snowplow/snowbridge/pkg/transform/transformconfig" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const url = "localhost:10000" | ||
|
||
func TestApp1(t *testing.T) { | ||
assert := assert.New(t) | ||
t.Setenv("SNOWBRIDGE_CONFIG_FILE", "./test_config.hcl") | ||
inputMessages := make(chan []string) | ||
outputMessages := []string{} | ||
wg := sync.WaitGroup{} | ||
|
||
run(inputMessages, &outputMessages, &wg) | ||
|
||
wg.Add(5) | ||
inputMessages <- []string{"mes1", "mes2"} | ||
time.Sleep(2 * time.Second) | ||
inputMessages <- []string{"mes3"} | ||
time.Sleep(2 * time.Second) | ||
inputMessages <- []string{"mes4", "mes5"} | ||
wg.Wait() | ||
|
||
assert.Equal([]string{"mes1", "mes2", "mes3", "mes4", "mes5"}, outputMessages) | ||
} | ||
|
||
func run(input chan []string, output *[]string, wg *sync.WaitGroup) { | ||
runHTTPServer(output, wg) | ||
|
||
sourceConfigPairs := []config.ConfigurationPair{inmemorysource.ConfigPair(input)} | ||
|
||
config, _, _ := cmd.Init() | ||
//TODO handle cancellation/stopping better | ||
go func() { | ||
err := cli.RunApp(config, sourceConfigPairs, transformconfig.SupportedTransformations) | ||
panic(err) | ||
}() | ||
} | ||
|
||
//Use httptest | ||
func runHTTPServer(output *[]string, wg *sync.WaitGroup) { | ||
mutex := &sync.Mutex{} | ||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | ||
defer req.Body.Close() | ||
data, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
mutex.Lock() | ||
*output = append(*output, string(data)) | ||
mutex.Unlock() | ||
defer wg.Done() | ||
}) | ||
go http.ListenAndServe(url, nil) | ||
} | ||
|
||
|
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 = "http://localhost:10000" | ||
} | ||
} | ||
|
||
source { | ||
use "inMemory" {} | ||
} | ||
|
||
license { | ||
accept = true | ||
} |