-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add channel name to websocket message
- Loading branch information
1 parent
d5127f3
commit 082a912
Showing
8 changed files
with
220 additions
and
22 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
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,164 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/celenium-io/astria-indexer/pkg/types" | ||
|
||
"github.com/celenium-io/astria-indexer/cmd/api/bus" | ||
"github.com/celenium-io/astria-indexer/cmd/api/handler/responses" | ||
ws "github.com/celenium-io/astria-indexer/cmd/api/handler/websocket" | ||
"github.com/celenium-io/astria-indexer/internal/storage" | ||
"github.com/celenium-io/astria-indexer/internal/storage/mock" | ||
"github.com/goccy/go-json" | ||
"github.com/gorilla/websocket" | ||
"github.com/labstack/echo/v4" | ||
"github.com/lib/pq" | ||
"github.com/rs/zerolog/log" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestWebsocket(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
listenerFactory := mock.NewMockListenerFactory(ctrl) | ||
listener := mock.NewMockListener(ctrl) | ||
|
||
listenerFactory.EXPECT().CreateListener().Return(listener).Times(1) | ||
|
||
headChannel := make(chan *pq.Notification, 10) | ||
listener.EXPECT().Listen().Return(headChannel).AnyTimes() | ||
listener.EXPECT().Subscribe(gomock.Any(), storage.ChannelHead).Return(nil).Times(1) | ||
listener.EXPECT().Close().Return(nil).MaxTimes(1) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
blockMock := mock.NewMockIBlock(ctrl) | ||
dispatcher, err := bus.NewDispatcher(listenerFactory, blockMock) | ||
require.NoError(t, err) | ||
dispatcher.Start(ctx) | ||
observer := dispatcher.Observe(storage.ChannelHead, storage.ChannelBlock) | ||
|
||
for i := 0; i < 10; i++ { | ||
hash := make([]byte, 32) | ||
_, err := rand.Read(hash) | ||
require.NoError(t, err) | ||
|
||
blockMock.EXPECT().ByIdWithRelations(ctx, uint64(i)).Return(storage.Block{ | ||
Id: uint64(i), | ||
Height: types.Level(i), | ||
Time: time.Now(), | ||
Hash: hash, | ||
Stats: testBlock.Stats, | ||
}, nil).MaxTimes(1) | ||
} | ||
|
||
go func() { | ||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
|
||
var id uint64 | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
id++ | ||
|
||
headChannel <- &pq.Notification{ | ||
Channel: storage.ChannelBlock, | ||
Extra: strconv.FormatUint(id, 10), | ||
} | ||
} | ||
} | ||
}() | ||
manager := ws.NewManager(observer) | ||
manager.Start(ctx) | ||
|
||
server := httptest.NewServer(http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
e := echo.New() | ||
c := e.NewContext(r, w) | ||
err := manager.Handle(c) | ||
require.NoError(t, err, "handle") | ||
<-ctx.Done() | ||
}, | ||
)) | ||
defer server.Close() | ||
|
||
wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/ws" | ||
dialed, _, err := websocket.DefaultDialer.Dial(wsURL, nil) | ||
require.NoError(t, err, "dial") | ||
|
||
body, err := json.Marshal(ws.Subscribe{ | ||
Channel: ws.ChannelBlocks, | ||
}) | ||
require.NoError(t, err, "marshal subscribe") | ||
|
||
err = dialed.WriteJSON(ws.Message{ | ||
Method: ws.MethodSubscribe, | ||
Body: body, | ||
}) | ||
require.NoError(t, err, "send subscribe message") | ||
|
||
ticker := time.NewTicker(time.Second * 5) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
body, err := json.Marshal(ws.Unsubscribe{ | ||
Channel: ws.ChannelHead, | ||
}) | ||
require.NoError(t, err, "marshal unsubscribe") | ||
|
||
err = dialed.WriteJSON(ws.Message{ | ||
Method: ws.MethodUnsubscribe, | ||
Body: body, | ||
}) | ||
require.NoError(t, err, "send unsubscribe message") | ||
|
||
err = dialed.Close() | ||
require.NoError(t, err, "closing connection") | ||
|
||
time.Sleep(time.Second) | ||
cancel() | ||
|
||
err = manager.Close() | ||
require.NoError(t, err, "closing manager") | ||
|
||
close(headChannel) | ||
return | ||
default: | ||
err := dialed.SetReadDeadline(time.Now().Add(time.Second * 3)) | ||
require.NoError(t, err) | ||
|
||
_, msg, err := dialed.ReadMessage() | ||
require.NoError(t, err, err) | ||
|
||
var notification ws.Notification[*responses.Block] | ||
err = json.Unmarshal(msg, ¬ification) | ||
require.NoError(t, err, err) | ||
|
||
require.Equal(t, ws.ChannelBlocks, notification.Channel) | ||
require.Greater(t, notification.Body.Id, uint64(0)) | ||
require.Greater(t, notification.Body.Height, uint64(0)) | ||
require.False(t, notification.Body.Time.IsZero()) | ||
require.Len(t, notification.Body.Hash, 32) | ||
|
||
log.Info(). | ||
Uint64("height", notification.Body.Height). | ||
Time("block_time", notification.Body.Time). | ||
Msg("new block") | ||
} | ||
} | ||
} |
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