Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PoPCHA flaky test #1743

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions be1-go/popcha/server_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package popcha

import (
"bufio"
"bytes"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"popstellar"
"popstellar/crypto"
"popstellar/hub"
Expand Down Expand Up @@ -262,9 +264,14 @@ func TestAuthorizationServerWebsocket(t *testing.T) {
// It tries to connect on /response endpoint without any additional path, and then
// tests the protocol with well-behaved clients using a valid path.
func TestAuthorizationServerWorkflow(t *testing.T) {
logTester := zltest.New(t)
logFile, err := os.CreateTemp("", "popcha_test_logs")
require.NoError(t, err)
defer func() {
logFile.Close()
os.Remove(logFile.Name())
}()

l := zerolog.New(logTester).With().Timestamp().Logger()
l := zerolog.New(logFile).With().Timestamp().Logger()
s, err := NewAuthServer(fakeHub{}, "localhost", 3005, l)
require.NoError(t, err)
s.Start()
Expand All @@ -277,9 +284,15 @@ func TestAuthorizationServerWorkflow(t *testing.T) {

// send any message to the websocket server
err = emptyPathClient.conn.WriteMessage(websocket.TextMessage, []byte("test"))
logTester.LastEntry().ExpMsg("Error while receiving a request on /response: empty path")
require.NoError(t, err)

// Read log file contents and check for the expected log message
err = logFile.Sync()
require.NoError(t, err)
lastLine, err := getLastLine(logFile.Name())
require.NoError(t, err)
require.Contains(t, lastLine, "Error while receiving a request on /response: empty path")

// create two clients, a sender and a receiver, on a valid path
validPath := strings.Join([]string{responseEndpoint, "laoid", "authentication", "clientid", "nonce"}, "/")

Expand Down Expand Up @@ -313,7 +326,12 @@ func TestAuthorizationServerWorkflow(t *testing.T) {

<-received

logTester.LastEntry().ExpLevel(zerolog.InfoLevel)
// Read log file contents and check for the expected log message
err = logFile.Sync()
require.NoError(t, err)
lastLine, err = getLastLine(logFile.Name())
require.NoError(t, err)
require.Contains(t, lastLine, "Received the correct message from the sender client.")

require.NoError(t, clientReceiver.conn.Close())
require.NoError(t, clientSender.conn.Close())
Expand Down Expand Up @@ -486,3 +504,24 @@ func (f *fakeResponseWriter) Write(_ []byte) (int, error) {
}

func (f *fakeResponseWriter) WriteHeader(_ int) {}

// getLastLine returns the last line of a file
func getLastLine(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()

var lastLine string
scanner := bufio.NewScanner(file)
i := 1
for scanner.Scan() {
lastLine = scanner.Text()
i++
}
if err = scanner.Err(); err != nil {
return "", err
}
return lastLine, scanner.Err()
}
Loading