Skip to content

Commit b7f23c6

Browse files
committed
basic tests
1 parent 057e220 commit b7f23c6

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

coordinator/internal/controller/proxy/client_manager.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ func (cliMgr *ClientManager) Client(ctx context.Context) *upClient {
132132
// Launch keep-login goroutine
133133
go func() {
134134
defer completionDone()
135-
expiredT := cliMgr.doLogin(context.Background(), loginCli)
136-
log.Info("login compeleted", "name", cliMgr.name, "expired", expiredT)
135+
cliMgr.doLogin(context.Background(), loginCli)
137136

138137
cliMgr.cachedCli.Lock()
139138
cliMgr.cachedCli.cli = loginCli

coordinator/test/proxy_test.go

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"strings"
89
"testing"
910
"time"
1011

1112
"github.com/gin-gonic/gin"
1213
"github.com/scroll-tech/da-codec/encoding"
1314
"github.com/stretchr/testify/assert"
1415

16+
"scroll-tech/common/types"
1517
"scroll-tech/common/types/message"
1618
"scroll-tech/common/version"
1719

@@ -151,30 +153,121 @@ func testProxyGetTask(t *testing.T) {
151153
assert.NoError(t, proxyHttpHandler.Shutdown(context.Background()))
152154
}()
153155

156+
chunkProver := newMockProver(t, "prover_chunk_test", proxyURL, message.ProofTypeChunk, version.Version)
157+
code, msg := chunkProver.tryGetProverTask(t, message.ProofTypeChunk)
158+
assert.Equal(t, int(types.ErrCoordinatorEmptyProofData), code)
159+
154160
err := l2BlockOrm.InsertL2Blocks(context.Background(), []*encoding.Block{block1, block2})
155161
assert.NoError(t, err)
156162
dbChunk, err := chunkOrm.InsertChunk(context.Background(), chunk)
157163
assert.NoError(t, err)
158164
err = l2BlockOrm.UpdateChunkHashInRange(context.Background(), 0, 100, dbChunk.Hash)
159165
assert.NoError(t, err)
160166

161-
time.Sleep(time.Second)
167+
task, code, msg := chunkProver.getProverTask(t, message.ProofTypeChunk)
168+
assert.Empty(t, code)
169+
if code == 0 {
170+
t.Log("get task id", task.TaskID)
171+
} else {
172+
t.Log("get task error msg", msg)
173+
}
174+
175+
}
176+
177+
func testProxyProof(t *testing.T) {
178+
urls := randmURLBatch(3)
179+
coordinatorURL0 := urls[0]
180+
collector0, httpHandler0 := setupCoordinator(t, 3, coordinatorURL0)
181+
defer func() {
182+
collector0.Stop()
183+
httpHandler0.Shutdown(context.Background())
184+
}()
185+
coordinatorURL1 := urls[1]
186+
collector1, httpHandler1 := setupCoordinator(t, 3, coordinatorURL1)
187+
defer func() {
188+
collector1.Stop()
189+
httpHandler1.Shutdown(context.Background())
190+
}()
191+
coordinators := map[string]*http.Server{
192+
"coordinator_0": httpHandler0,
193+
"coordinator_1": httpHandler1,
194+
}
195+
196+
proxyURL := urls[2]
197+
proxyHttpHandler := setupProxy(t, proxyURL, []string{coordinatorURL0, coordinatorURL1})
198+
defer func() {
199+
fmt.Println("px end start")
200+
assert.NoError(t, proxyHttpHandler.Shutdown(context.Background()))
201+
fmt.Println("px end")
202+
}()
203+
204+
err := l2BlockOrm.InsertL2Blocks(context.Background(), []*encoding.Block{block1, block2})
205+
assert.NoError(t, err)
206+
dbChunk, err := chunkOrm.InsertChunk(context.Background(), chunk)
207+
assert.NoError(t, err)
208+
err = l2BlockOrm.UpdateChunkHashInRange(context.Background(), 0, 100, dbChunk.Hash)
209+
assert.NoError(t, err)
162210

163211
chunkProver := newMockProver(t, "prover_chunk_test", proxyURL, message.ProofTypeChunk, version.Version)
164212
task, code, msg := chunkProver.getProverTask(t, message.ProofTypeChunk)
165213
assert.Empty(t, code)
166214
if code == 0 {
167-
t.Log("get task id", task.TaskID)
215+
t.Log("get task", task)
216+
parts, _, _ := strings.Cut(task.TaskID, ":")
217+
// close the coordinator which do not dispatch task first, so if we submit to wrong target,
218+
// there would be a chance the submit failed (to the closed coordinator)
219+
for n, srv := range coordinators {
220+
if n != parts {
221+
t.Log("close coordinator", n)
222+
assert.NoError(t, srv.Shutdown(context.Background()))
223+
}
224+
}
225+
exceptProofStatus := verifiedSuccess
226+
chunkProver.submitProof(t, task, exceptProofStatus, types.Success)
227+
168228
} else {
169229
t.Log("get task error msg", msg)
170230
}
231+
232+
// verify proof status
233+
var (
234+
tick = time.Tick(1500 * time.Millisecond)
235+
tickStop = time.Tick(time.Minute)
236+
)
237+
238+
var (
239+
chunkProofStatus types.ProvingStatus
240+
chunkActiveAttempts int16
241+
chunkMaxAttempts int16
242+
)
243+
244+
for {
245+
select {
246+
case <-tick:
247+
chunkProofStatus, err = chunkOrm.GetProvingStatusByHash(context.Background(), dbChunk.Hash)
248+
assert.NoError(t, err)
249+
if chunkProofStatus == types.ProvingTaskVerified {
250+
return
251+
}
252+
253+
chunkActiveAttempts, chunkMaxAttempts, err = chunkOrm.GetAttemptsByHash(context.Background(), dbChunk.Hash)
254+
assert.NoError(t, err)
255+
assert.Equal(t, 1, int(chunkMaxAttempts))
256+
assert.Equal(t, 0, int(chunkActiveAttempts))
257+
258+
case <-tickStop:
259+
t.Error("failed to check proof status", "chunkProofStatus", chunkProofStatus.String())
260+
return
261+
}
262+
}
171263
}
172264

173265
func TestProxyClient(t *testing.T) {
174266

175267
// Set up the test environment.
176268
setEnv(t)
177-
//t.Run("TestProxyClient", testProxyClient)
178-
//t.Run("TestProxyHandshake", testProxyHandshake)
269+
t.Run("TestProxyClient", testProxyClient)
270+
t.Run("TestProxyHandshake", testProxyHandshake)
179271
t.Run("TestProxyGetTask", testProxyGetTask)
272+
t.Run("TestProxyValidProof", testProxyProof)
180273
}

0 commit comments

Comments
 (0)