-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathmoc_test.go
More file actions
83 lines (70 loc) · 2.1 KB
/
Copy pathmoc_test.go
File metadata and controls
83 lines (70 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2026 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api_test
import (
"encoding/hex"
"fmt"
"testing"
"time"
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/moc"
mockbatchstore "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock"
"github.com/ethersphere/bee/v2/pkg/soc"
mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/util/testutil"
"github.com/gorilla/websocket"
)
// TestMocWebsocketSingleHandler subscribes on a SOC id and receives a message.
func TestMocWebsocketSingleHandler(t *testing.T) {
t.Parallel()
var (
id = make([]byte, 32)
m, cl, signer = newMocTest(t, id, 0)
respC = make(chan error, 1)
payload = []byte("hello there!")
)
err := cl.SetReadDeadline(time.Now().Add(longTimeout))
if err != nil {
t.Fatal(err)
}
cl.SetReadLimit(swarm.ChunkSize)
ch, _ := cac.New(payload)
socCh := soc.New(id, ch)
ch, _ = socCh.Sign(signer)
socCh, _ = soc.FromChunk(ch)
m.Handle(socCh)
go expectMessage(t, cl, respC, payload)
if err := <-respC; err != nil {
t.Fatal(err)
}
}
func newMocTest(t *testing.T, socId []byte, pingPeriod time.Duration) (moc.Listener, *websocket.Conn, crypto.Signer) {
t.Helper()
if pingPeriod == 0 {
pingPeriod = 10 * time.Second
}
var (
batchStore = mockbatchstore.New()
storer = mockstorer.New()
)
privKey, err := crypto.GenerateSecp256k1Key()
if err != nil {
t.Fatal(err)
}
signer := crypto.NewDefaultSigner(privKey)
mocService := moc.New(log.NewLogger("test"))
testutil.CleanupCloser(t, mocService)
_, cl, _, _ := newTestServer(t, testServerOptions{
Moc: mocService,
WsPath: fmt.Sprintf("/moc/subscribe/%s", hex.EncodeToString(socId)),
Storer: storer,
BatchStore: batchStore,
Logger: log.Noop,
WsPingPeriod: pingPeriod,
})
return mocService, cl, signer
}