Skip to content

Commit 27826b3

Browse files
committed
Various improvements to integration tests
This has been a long time coming, but it's still not perfect. Basically I'm trying to reset the entire environment as much as possible so that each spec runs in a clean room. Mostly in this commit Firefox is being killed and restarted for every spec, which has made a lot of improvements.
1 parent 8ab587d commit 27826b3

File tree

12 files changed

+161
-47
lines changed

12 files changed

+161
-47
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go:
55

66
addons:
77
# Use the full version number with ".0" if needed. This value is scraped by setup scripts.
8-
firefox: "66.0.4"
8+
firefox: "67.0.1"
99
apt:
1010
packages:
1111
- rpm
@@ -37,8 +37,8 @@ install:
3737
script:
3838
- cd $REPO_ROOT/webext && npm test
3939
- cd $REPO_ROOT/interfacer && go test $(find src/browsh -name *.go | grep -v windows)
40-
- cd $REPO_ROOT/interfacer && go test test/tty/*.go -v -ginkgo.flakeAttempts=3
41-
- cd $REPO_ROOT/interfacer && go test test/http-server/*.go -v
40+
- cd $REPO_ROOT/interfacer && go test test/tty/*.go -v -ginkgo.slowSpecThreshold=30 -ginkgo.flakeAttempts=3
41+
- cd $REPO_ROOT/interfacer && go test test/http-server/*.go -v -ginkgo.slowSpecThreshold=30 -ginkgo.flakeAttempts=3
4242
after_failure:
4343
- cat $REPO_ROOT/interfacer/test/tty/debug.log
4444
- cat $REPO_ROOT/interfacer/test/http-server/debug.log

interfacer/src/browsh/browsh.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
var (
2525
logo = `
26-
//// ////
26+
//// ////
2727
/ / / /
2828
// //
2929
// // ,,,,,,,,
@@ -138,9 +138,9 @@ func Shell(command string) string {
138138
parts = parts[1:]
139139
out, err := exec.Command(head, parts...).CombinedOutput()
140140
if err != nil {
141-
Log(fmt.Sprintf(
142-
"Browsh tried to run `%s` but failed with: %s", command, string(out)))
143-
Shutdown(err)
141+
errorMessge := fmt.Sprintf(
142+
"Browsh tried to run `%s` but failed with: %s", command, string(out))
143+
Shutdown(errors.New(errorMessge))
144144
}
145145
return strings.TrimSpace(string(out))
146146
}
@@ -151,7 +151,7 @@ func TTYStart(injectedScreen tcell.Screen) {
151151
setupTcell()
152152
writeString(1, 0, logo, tcell.StyleDefault)
153153
writeString(0, 15, "Starting Browsh v"+browshVersion+", the modern text-based web browser.", tcell.StyleDefault)
154-
startFirefox()
154+
StartFirefox()
155155
Log("Starting Browsh CLI client")
156156
go readStdin()
157157
startWebSocketServer()

interfacer/src/browsh/comms.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"strings"
88

9+
"github.com/go-errors/errors"
910
"github.com/gorilla/websocket"
1011
"github.com/spf13/viper"
1112
)
@@ -17,7 +18,7 @@ var (
1718
WriteBufferSize: 1024,
1819
}
1920
stdinChannel = make(chan string)
20-
isConnectedToWebExtension = false
21+
IsConnectedToWebExtension = false
2122
)
2223

2324
type incomingRawText struct {
@@ -29,13 +30,14 @@ func startWebSocketServer() {
2930
serverMux := http.NewServeMux()
3031
serverMux.HandleFunc("/", webSocketServer)
3132
port := viper.GetString("browsh.websocket-port")
32-
if err := http.ListenAndServe(":"+port, serverMux); err != nil {
33-
Shutdown(err)
33+
Log("Starting websocket server...")
34+
if netErr := http.ListenAndServe(":"+port, serverMux); netErr != nil {
35+
Shutdown(errors.New(fmt.Errorf("Error starting websocket server: %v", netErr)))
3436
}
3537
}
3638

3739
func sendMessageToWebExtension(message string) {
38-
if !isConnectedToWebExtension {
40+
if !IsConnectedToWebExtension {
3941
Log("Webextension not connected. Message not sent: " + message)
4042
return
4143
}
@@ -55,6 +57,11 @@ func webSocketReader(ws *websocket.Conn) {
5557
triggerSocketWriterClose()
5658
return
5759
}
60+
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
61+
Log("Socket reader detected that the connection unexpectedly dissapeared")
62+
triggerSocketWriterClose()
63+
return
64+
}
5865
Shutdown(err)
5966
}
6067
}
@@ -125,9 +132,10 @@ func webSocketWriter(ws *websocket.Conn) {
125132
if err := ws.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
126133
if err == websocket.ErrCloseSent {
127134
Log("Socket writer detected that the browser closed the websocket")
128-
return
135+
} else {
136+
Log("Socket writer detected unexpected closure of websocket")
129137
}
130-
Shutdown(err)
138+
return
131139
}
132140
}
133141
}
@@ -138,7 +146,7 @@ func webSocketServer(w http.ResponseWriter, r *http.Request) {
138146
if err != nil {
139147
Shutdown(err)
140148
}
141-
isConnectedToWebExtension = true
149+
IsConnectedToWebExtension = true
142150
go webSocketWriter(ws)
143151
go webSocketReader(ws)
144152
sendConfigToWebExtension()

interfacer/src/browsh/firefox.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ var (
5252
)
5353

5454
func startHeadlessFirefox() {
55-
checkIfFirefoxIsAlreadyRunning()
5655
Log("Starting Firefox in headless mode")
56+
checkIfFirefoxIsAlreadyRunning()
5757
firefoxPath := ensureFirefoxBinary()
5858
ensureFirefoxVersion(firefoxPath)
5959
args := []string{"--marionette"}
@@ -149,6 +149,10 @@ func versionOrdinal(version string) string {
149149
// extension.
150150
func startWERFirefox() {
151151
Log("Attempting to start headless Firefox with `web-ext`")
152+
if IsConnectedToWebExtension {
153+
Shutdown(errors.New("There appears to already be an existing Web Extension connection"))
154+
}
155+
checkIfFirefoxIsAlreadyRunning()
152156
var rootDir = Shell("git rev-parse --show-toplevel")
153157
args := []string{
154158
"run",
@@ -158,7 +162,6 @@ func startWERFirefox() {
158162
}
159163
firefoxProcess := exec.Command(rootDir+"/webext/node_modules/.bin/web-ext", args...)
160164
firefoxProcess.Dir = rootDir + "/webext/dist/"
161-
defer firefoxProcess.Process.Kill()
162165
stdout, err := firefoxProcess.StdoutPipe()
163166
if err != nil {
164167
Shutdown(err)
@@ -168,13 +171,16 @@ func startWERFirefox() {
168171
}
169172
in := bufio.NewScanner(stdout)
170173
for in.Scan() {
174+
if strings.Contains(in.Text(), "Connected to the remote Firefox debugger") {
175+
}
171176
if strings.Contains(in.Text(), "JavaScript strict") ||
172177
strings.Contains(in.Text(), "D-BUS") ||
173178
strings.Contains(in.Text(), "dbus") {
174179
continue
175180
}
176181
Log("FF-CONSOLE: " + in.Text())
177182
}
183+
Log("WER Firefox unexpectedly closed")
178184
}
179185

180186
// Connect to Firefox's Marionette service.
@@ -294,7 +300,7 @@ func setupFirefox() {
294300
installWebextension()
295301
}
296302

297-
func startFirefox() {
303+
func StartFirefox() {
298304
if !viper.GetBool("firefox.use-existing") {
299305
writeString(0, 16, "Waiting for Firefox to connect...", tcell.StyleDefault)
300306
if IsTesting {

interfacer/src/browsh/raw_text_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type rawTextResponse struct {
6666
// it will return:
6767
// `Something `
6868
func HTTPServerStart() {
69-
startFirefox()
69+
StartFirefox()
7070
go startWebSocketServer()
7171
Log("Starting Browsh HTTP server")
7272
bind := viper.GetString("http-server.bind")

interfacer/src/browsh/tab.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ type tab struct {
2929
frame frame
3030
}
3131

32+
func ResetTabs() {
33+
Tabs = make(map[int]*tab)
34+
CurrentTab = nil
35+
tabsOrder = nil
36+
tabsDeleted = nil
37+
}
38+
3239
func ensureTabExists(id int) {
3340
if _, ok := Tabs[id]; !ok {
3441
newTab(id)

interfacer/src/browsh/ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func writeString(x, y int, str string, style tcell.Style) {
3737
x = xOriginal
3838
continue
3939
}
40-
screen.SetContent(x, y, c, nil, style)
40+
screen.SetCell(x, y, style, c)
4141
x++
4242
}
4343
}

interfacer/test/http-server/setup.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ func startStaticFileServer() {
2121
http.ListenAndServe(":"+staticFileServerPort, serverMux)
2222
}
2323

24-
func startBrowsh() {
24+
func initBrowsh() {
2525
browsh.IsTesting = true
2626
browsh.Initialise()
2727
viper.Set("http-server-mode", true)
28-
browsh.HTTPServerStart()
28+
}
29+
30+
func waitUntilConnectedToWebExtension(maxTime time.Duration) {
31+
start := time.Now()
32+
for time.Since(start) < maxTime {
33+
if browsh.IsConnectedToWebExtension {
34+
return
35+
}
36+
time.Sleep(50 * time.Millisecond)
37+
}
38+
panic("Didn't connect to webextension in time")
2939
}
3040

3141
func getBrowshServiceBase() string {
@@ -55,22 +65,31 @@ func getPath(path string, mode string) string {
5565
}
5666
}
5767

68+
func stopFirefox() {
69+
browsh.IsConnectedToWebExtension = false
70+
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
71+
time.Sleep(500 * time.Millisecond)
72+
}
73+
5874
var _ = ginkgo.BeforeEach(func() {
75+
stopFirefox()
76+
browsh.ResetTabs()
77+
browsh.StartFirefox()
78+
waitUntilConnectedToWebExtension(15 * time.Second)
5979
browsh.IsMonochromeMode = false
6080
browsh.Log("\n---------")
6181
browsh.Log(ginkgo.CurrentGinkgoTestDescription().FullTestText)
6282
browsh.Log("---------")
6383
})
6484

6585
var _ = ginkgo.BeforeSuite(func() {
86+
initBrowsh()
87+
stopFirefox()
6688
go startStaticFileServer()
67-
go startBrowsh()
68-
time.Sleep(15 * time.Second)
69-
// Allow the browser to sort its sizing out, because sometimes the first test catches the
70-
// browser before it's completed its resizing.
71-
getPath("/smorgasbord", "plain")
89+
go browsh.HTTPServerStart()
90+
time.Sleep(1 * time.Second)
7291
})
7392

7493
var _ = ginkgo.AfterSuite(func() {
75-
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
94+
stopFirefox()
7695
})

interfacer/test/tty/setup.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package test
22

33
import (
4+
"fmt"
45
"net/http"
6+
"os"
7+
"path/filepath"
58
"strconv"
69
"time"
710
"unicode/utf8"
@@ -22,6 +25,8 @@ var perTestTimeout = 2000 * time.Millisecond
2225
var rootDir = browsh.Shell("git rev-parse --show-toplevel")
2326
var testSiteURL = "http://localhost:" + staticFileServerPort
2427
var ti *terminfo.Terminfo
28+
var dir, _ = os.Getwd()
29+
var framesLogFile = fmt.Sprintf(filepath.Join(dir, "frames.log"))
2530

2631
func initTerm() {
2732
// The tests check for true colour RGB values. The only downside to forcing true colour
@@ -48,11 +53,25 @@ func GetFrame() string {
4853
line = 0
4954
}
5055
}
56+
writeFrameLog("================================================")
57+
writeFrameLog(ginkgo.CurrentGinkgoTestDescription().FullTestText)
58+
writeFrameLog("================================================\n")
5159
log = "\n" + log + styleDefault
52-
ginkgo.GinkgoWriter.Write([]byte(log))
60+
writeFrameLog(log)
5361
return frame
5462
}
5563

64+
func writeFrameLog(log string) {
65+
f, err := os.OpenFile(framesLogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
66+
if err != nil {
67+
panic(err)
68+
}
69+
defer f.Close()
70+
if _, err = f.WriteString(log); err != nil {
71+
panic(err)
72+
}
73+
}
74+
5675
// Trigger the key definition specified by name
5776
func triggerUserKeyFor(name string) {
5877
key := viper.GetStringSlice(name)
@@ -110,7 +129,7 @@ func WaitForPageLoad() {
110129

111130
func sleepUntilPageLoad(maxTime time.Duration) {
112131
start := time.Now()
113-
time.Sleep(50 * time.Millisecond)
132+
time.Sleep(1000 * time.Millisecond)
114133
for time.Since(start) < maxTime {
115134
if browsh.CurrentTab != nil {
116135
if browsh.CurrentTab.PageState == "parsing_complete" {
@@ -132,6 +151,12 @@ func GotoURL(url string) {
132151
// TODO: Looking for the URL isn't optimal because it could be the same URL
133152
// as the previous test.
134153
gomega.Expect(url).To(BeInFrameAt(0, 1))
154+
// TODO: hack to work around bug where text sometimes doesn't render on page load.
155+
// Clicking with the mouse triggers a reparse by the web extension
156+
mouseClick(3, 6)
157+
time.Sleep(100 * time.Millisecond)
158+
mouseClick(3, 6)
159+
time.Sleep(500 * time.Millisecond)
135160
}
136161

137162
func mouseClick(x, y int) {
@@ -199,31 +224,49 @@ func startStaticFileServer() {
199224
http.ListenAndServe(":"+staticFileServerPort, serverMux)
200225
}
201226

202-
func startBrowsh() {
227+
func initBrowsh() {
203228
browsh.IsTesting = true
204229
simScreen = tcell.NewSimulationScreen("UTF-8")
205230
browsh.Initialise()
206-
browsh.TTYStart(simScreen)
231+
232+
}
233+
234+
func stopFirefox() {
235+
browsh.Log("Attempting to kill all firefox processes")
236+
browsh.IsConnectedToWebExtension = false
237+
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
238+
time.Sleep(500 * time.Millisecond)
207239
}
208240

209241
func runeCount(text string) int {
210242
return utf8.RuneCountInString(text)
211243
}
212244

213245
var _ = ginkgo.BeforeEach(func() {
246+
browsh.Log("Attempting to restart WER Firefox...")
247+
stopFirefox()
248+
browsh.ResetTabs()
249+
browsh.StartFirefox()
250+
sleepUntilPageLoad(startupWait)
214251
browsh.IsMonochromeMode = false
215252
browsh.Log("\n---------")
216253
browsh.Log(ginkgo.CurrentGinkgoTestDescription().FullTestText)
217254
browsh.Log("---------")
218255
})
219256

220257
var _ = ginkgo.BeforeSuite(func() {
258+
os.Truncate(framesLogFile, 0)
221259
initTerm()
260+
initBrowsh()
261+
stopFirefox()
222262
go startStaticFileServer()
223-
go startBrowsh()
224-
sleepUntilPageLoad(startupWait)
263+
go browsh.TTYStart(simScreen)
264+
// Firefox seems to take longer to die after its first run
265+
time.Sleep(500 * time.Millisecond)
266+
stopFirefox()
267+
time.Sleep(5000 * time.Millisecond)
225268
})
226269

227270
var _ = ginkgo.AfterSuite(func() {
228-
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
271+
stopFirefox()
229272
})

0 commit comments

Comments
 (0)