-
Notifications
You must be signed in to change notification settings - Fork 16
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
1 parent
66bf221
commit 0703867
Showing
18 changed files
with
286 additions
and
274 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/go-plugin" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-relay/pkg/utils" | ||
) | ||
|
||
type HelperProcessOptions struct { | ||
Limit int | ||
} | ||
|
||
func HelperProcess(commandLocation string, command string, opts HelperProcessOptions) *exec.Cmd { | ||
cmdArgs := []string{ | ||
"go", "run", commandLocation, fmt.Sprintf("-cmd=%s", command), | ||
} | ||
if opts.Limit != 0 { | ||
cmdArgs = append(cmdArgs, fmt.Sprintf("-limit=%d", opts.Limit)) | ||
} | ||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) | ||
cmd.Env = append(os.Environ()) | ||
return cmd | ||
} | ||
|
||
func PluginTest[I any](t *testing.T, name string, p plugin.Plugin, testFn func(*testing.T, I)) { | ||
ctx, cancel := context.WithCancel(utils.Context(t)) | ||
defer cancel() | ||
|
||
ch := make(chan *plugin.ReattachConfig, 1) | ||
closeCh := make(chan struct{}) | ||
go plugin.Serve(&plugin.ServeConfig{ | ||
Test: &plugin.ServeTestConfig{ | ||
Context: ctx, | ||
ReattachConfigCh: ch, | ||
CloseCh: closeCh, | ||
}, | ||
GRPCServer: plugin.DefaultGRPCServer, | ||
Plugins: map[string]plugin.Plugin{name: p}, | ||
}) | ||
|
||
// We should get a config | ||
var config *plugin.ReattachConfig | ||
select { | ||
case config = <-ch: | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("should've received reattach") | ||
} | ||
require.NotNil(t, config) | ||
|
||
c := plugin.NewClient(&plugin.ClientConfig{ | ||
Reattach: config, | ||
Plugins: map[string]plugin.Plugin{name: p}, | ||
}) | ||
t.Cleanup(c.Kill) | ||
clientProtocol, err := c.Client() | ||
require.NoError(t, err) | ||
defer clientProtocol.Close() | ||
i, err := clientProtocol.Dispense(name) | ||
require.NoError(t, err) | ||
|
||
testFn(t, i.(I)) | ||
|
||
// stop plugin | ||
cancel() | ||
select { | ||
case <-closeCh: | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("should've stopped") | ||
} | ||
require.Error(t, clientProtocol.Ping()) | ||
} |
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
Oops, something went wrong.