-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Testing, Tooling] Expose integration app via gRPC/HTTP/WS #1017
Draft
bryanchriswhite
wants to merge
13
commits into
main
Choose a base branch
from
test/module-e2e
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3cf4cd3
wip
bryanchriswhite 3d3e3f4
wip
bryanchriswhite 9a5fc9d
fix: Working grpc server
red-0ne 1714822
wip
bryanchriswhite f9091a3
wip
bryanchriswhite c76a958
wip
bryanchriswhite 423067b
wip: some cleanup
bryanchriswhite 2b76ef1
wip
bryanchriswhite 0b1ff7f
wip
bryanchriswhite b975758
wip
bryanchriswhite a1bbe17
wip: cleanup
bryanchriswhite 2777eaa
wip
bryanchriswhite 5de0cea
chore: cleanup
bryanchriswhite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"sync" | ||
"testing" | ||
|
||
comettypes "github.com/cometbft/cometbft/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
"github.com/gorilla/websocket" | ||
"github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
"github.com/grpc-ecosystem/grpc-gateway/utilities" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
coretypes "github.com/cometbft/cometbft/rpc/core/types" | ||
|
||
"github.com/pokt-network/poktroll/testutil/integration" | ||
"github.com/pokt-network/poktroll/testutil/testclient" | ||
) | ||
|
||
// E2EApp wraps an integration.App and provides both gRPC and WebSocket servers for end-to-end testing | ||
type E2EApp struct { | ||
*integration.App | ||
grpcServer *grpc.Server | ||
grpcListener net.Listener | ||
wsServer *http.Server | ||
wsListener net.Listener | ||
wsUpgrader websocket.Upgrader | ||
wsConnMutex sync.RWMutex | ||
wsConnections map[*websocket.Conn]map[string]struct{} // maps connections to their subscribed event queries | ||
resultEventChan chan *coretypes.ResultEvent | ||
} | ||
|
||
// NewE2EApp creates a new E2EApp instance with integration.App, gRPC, and WebSocket servers | ||
func NewE2EApp(t *testing.T, opts ...integration.IntegrationAppOptionFn) *E2EApp { | ||
t.Helper() | ||
|
||
// Initialize and start gRPC server | ||
creds := insecure.NewCredentials() | ||
grpcServer := grpc.NewServer(grpc.Creds(creds)) | ||
mux := runtime.NewServeMux() | ||
|
||
rootPattern, err := runtime.NewPattern( | ||
1, | ||
[]int{int(utilities.OpLitPush), int(utilities.OpNop)}, | ||
[]string{""}, | ||
"", | ||
) | ||
require.NoError(t, err) | ||
|
||
// Create the integration app | ||
opts = append(opts, integration.WithGRPCServer(grpcServer)) | ||
app := integration.NewCompleteIntegrationApp(t, opts...) | ||
app.RegisterGRPCServer(grpcServer) | ||
|
||
flagSet := testclient.NewFlagSet(t, "tcp://127.0.0.1:42070") | ||
keyRing := keyring.NewInMemory(app.GetCodec()) | ||
clientCtx := testclient.NewLocalnetClientCtx(t, flagSet).WithKeyring(keyRing) | ||
|
||
// Register the handler with the mux | ||
client, err := grpc.NewClient("127.0.0.1:42069", grpc.WithInsecure()) | ||
require.NoError(t, err) | ||
|
||
for _, mod := range app.GetModuleManager().Modules { | ||
mod.(module.AppModuleBasic).RegisterGRPCGatewayRoutes(clientCtx, mux) | ||
} | ||
|
||
// Create listeners for gRPC, WebSocket, and HTTP | ||
grpcListener, err := net.Listen("tcp", "127.0.0.1:42069") | ||
require.NoError(t, err, "failed to create gRPC listener") | ||
|
||
wsListener, err := net.Listen("tcp", "127.0.0.1:6969") | ||
require.NoError(t, err, "failed to create WebSocket listener") | ||
|
||
e2eApp := &E2EApp{ | ||
App: app, | ||
grpcListener: grpcListener, | ||
grpcServer: grpcServer, | ||
wsListener: wsListener, | ||
wsConnections: make(map[*websocket.Conn]map[string]struct{}), | ||
wsUpgrader: websocket.Upgrader{}, | ||
resultEventChan: make(chan *coretypes.ResultEvent), | ||
} | ||
|
||
mux.Handle(http.MethodPost, rootPattern, newPostHandler(client, e2eApp)) | ||
|
||
go func() { | ||
if err := e2eApp.grpcServer.Serve(grpcListener); err != nil { | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
panic(err) | ||
} | ||
} | ||
}() | ||
|
||
// Initialize and start WebSocket server | ||
e2eApp.wsServer = newWebSocketServer(e2eApp) | ||
go func() { | ||
if err := e2eApp.wsServer.Serve(wsListener); err != nil && errors.Is(err, http.ErrServerClosed) { | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
panic(err) | ||
} | ||
} | ||
}() | ||
|
||
// Initialize and start HTTP server | ||
go func() { | ||
if err := http.ListenAndServe("127.0.0.1:42070", mux); err != nil { | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
panic(err) | ||
} | ||
} | ||
}() | ||
|
||
// Start event handling | ||
go e2eApp.handleResultEvents(t) | ||
|
||
return e2eApp | ||
} | ||
|
||
// Close gracefully shuts down the E2EApp and its servers | ||
func (app *E2EApp) Close() error { | ||
app.grpcServer.GracefulStop() | ||
if err := app.wsServer.Close(); err != nil { | ||
return err | ||
} | ||
|
||
close(app.resultEventChan) | ||
|
||
return nil | ||
} | ||
|
||
// GetClientConn returns a gRPC client connection to the E2EApp's gRPC server | ||
func (app *E2EApp) GetClientConn(ctx context.Context) (*grpc.ClientConn, error) { | ||
return grpc.DialContext( | ||
ctx, | ||
app.grpcListener.Addr().String(), | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
) | ||
} | ||
|
||
// GetWSEndpoint returns the WebSocket endpoint URL | ||
func (app *E2EApp) GetWSEndpoint() string { | ||
return "ws://" + app.wsListener.Addr().String() + "/websocket" | ||
} | ||
|
||
// TODO_IN_THIS_COMMIT: godoc & move... | ||
func (app *E2EApp) GetCometBlockID() comettypes.BlockID { | ||
lastBlockID := app.GetSdkCtx().BlockHeader().LastBlockId | ||
partSetHeader := lastBlockID.GetPartSetHeader() | ||
|
||
return comettypes.BlockID{ | ||
Hash: lastBlockID.GetHash(), | ||
PartSetHeader: comettypes.PartSetHeader{ | ||
Total: partSetHeader.GetTotal(), | ||
Hash: partSetHeader.GetHash(), | ||
}, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc & move...