summaryrefslogtreecommitdiff
path: root/cmd/tealdbg/server_test.go
blob: aa138b30b3d29053c8dedc1599b5215aa4eb077f (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"context"
	"math/rand"
	"strings"
	"testing"
	"time"

	"github.com/gorilla/mux"
	"github.com/stretchr/testify/require"
)

type mockFactory struct{}

type testServerDebugFrontend struct {
	debugger      Control
	notifications chan Notification
}

func (t testServerDebugFrontend) SessionStarted(sid string, debugger Control, ch chan Notification) {
	t.notifications = ch
	t.debugger = debugger
	go t.eventLoop()
}

func (t testServerDebugFrontend) SessionEnded(sid string) {
}

func (t testServerDebugFrontend) WaitForCompletion() {
}

func (t testServerDebugFrontend) URL() string {
	return ""
}

func (t testServerDebugFrontend) eventLoop() {
	for {
		select {
		case n := <-t.notifications:
			if n.Event == "completed" {
				return
			}
			if n.Event == "registered" {
			}
			// simulate user delay to workaround race cond
			time.Sleep(10 * time.Millisecond)
			t.debugger.Resume()
		}
	}
}

func (f *mockFactory) Make(router *mux.Router, appAddress string) (da DebugAdapter) {
	return testServerDebugFrontend{}
}

func tryStartingServerRemote(t *testing.T, ds *DebugServer) (ok bool) {
	res := make(chan bool)
	go func() {
		defer func() {
			if r := recover(); r != nil {
				res <- false
			}
		}()
		err := ds.startRemote()
		require.NoError(t, err)
		res <- true
	}()

	time.Sleep(500 * time.Millisecond)
	err := ds.server.Shutdown(context.Background())
	require.NoError(t, err)

	ok = <-res
	return
}

func tryStartingServerDebug(t *testing.T, ds *DebugServer) (ok bool) {
	defer func() {
		if r := recover(); r != nil {
			ok = false
		}
	}()
	err := ds.startDebug()
	require.NoError(t, err)
	ok = true
	return
}

func serverTestImpl(t *testing.T, run func(t *testing.T, ds *DebugServer) bool, dp *DebugParams) {
	maxPortNum := 65000
	minPortNum := 40000
	attempt := 0
	started := false
	var ds DebugServer
	for attempt < 5 && !started {
		port = rand.Intn(maxPortNum-minPortNum) + minPortNum
		ds = makeDebugServer("127.0.0.1", port, &mockFactory{}, dp)
		started = run(t, &ds)
		attempt++
	}

	require.True(t, started)
	require.NotEmpty(t, ds)
	require.NotNil(t, ds.debugger)
	require.NotNil(t, ds.router)
	require.NotNil(t, ds.server)
}

func TestServerRemote(t *testing.T) {
	serverTestImpl(t, tryStartingServerRemote, &DebugParams{})
}

func TestServerLocal(t *testing.T) {
	txnBlob := []byte("[" + strings.Join([]string{string(txnSample), txnSample}, ",") + "]")
	dp := DebugParams{
		ProgramNames: []string{"test"},
		ProgramBlobs: [][]byte{{2, 0x20, 1, 1, 0x22}}, // version, intcb, int 1
		TxnBlob:      txnBlob,
		GroupIndex:   0,
		RunMode:      "signature",
	}

	serverTestImpl(t, tryStartingServerDebug, &dp)
}