| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func waitForTabState(t *testing.T, a *App, tabID, want string) { |
| 11 | t.Helper() |
| 12 | deadline := time.Now().Add(3 * time.Second) |
| 13 | for { |
| 14 | a.remoteTabMu.Lock() |
| 15 | tab := a.remoteTabs[tabID] |
| 16 | state := "" |
| 17 | if tab != nil { |
| 18 | state = tab.state |
| 19 | } |
| 20 | a.remoteTabMu.Unlock() |
| 21 | if state == want { |
| 22 | return |
| 23 | } |
| 24 | if time.Now().After(deadline) { |
| 25 | t.Fatalf("remote tab %s state = %q, want %q", tabID, state, want) |
| 26 | } |
| 27 | time.Sleep(10 * time.Millisecond) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func waitForRemoteTabError(t *testing.T, a *App, tabID, want string) { |
| 32 | t.Helper() |
| 33 | deadline := time.Now().Add(3 * time.Second) |
| 34 | for { |
| 35 | a.remoteTabMu.Lock() |
| 36 | tab := a.remoteTabs[tabID] |
| 37 | message := "" |
| 38 | if tab != nil { |
| 39 | message = tab.err |
| 40 | } |
| 41 | a.remoteTabMu.Unlock() |
| 42 | if strings.Contains(message, want) { |
| 43 | return |
| 44 | } |
| 45 | if time.Now().After(deadline) { |
| 46 | t.Fatalf("remote tab %s error = %q, want text %q", tabID, message, want) |
| 47 | } |
| 48 | time.Sleep(10 * time.Millisecond) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // resumedSessionID reports the identity the last /resume carried. |
| 53 | func (fs *fakeServe) resumedSessionID() string { |
| 54 | fs.mu.Lock() |
| 55 | defer fs.mu.Unlock() |
| 56 | return fs.resumeSessionID |
| 57 | } |
| 58 | |
| 59 | // takeFault consumes one scheduled fault from a fakeServe counter. Caller |
| 60 | // holds fs.mu. |
| 61 | func (fs *fakeServe) takeFault(counter *int) bool { |
| 62 | if *counter <= 0 { |
| 63 | return false |
| 64 | } |
| 65 | *counter-- |
| 66 | return true |
| 67 | } |
| 68 | |
| 69 | // dropHTTPConnection ends a request the way a dying tunnel does: the client |
| 70 | // sees a transport error, never a status, and cannot tell whether Serve |
| 71 | // committed the request. |
| 72 | func dropHTTPConnection(w http.ResponseWriter) { |
| 73 | hijacker, ok := w.(http.Hijacker) |
| 74 | if !ok { |
| 75 | panic("fake serve cannot hijack the connection") |
| 76 | } |
| 77 | conn, _, err := hijacker.Hijack() |
| 78 | if err != nil { |
| 79 | panic(err) |
| 80 | } |
| 81 | conn.Close() |
| 82 | } |
| 83 |