|
| 1 | +package blcu_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction" |
| 7 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/broker" |
| 8 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/blcu" |
| 9 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/tests_functions" |
| 10 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/websocket" |
| 11 | + ws "github.com/gorilla/websocket" |
| 12 | + "github.com/rs/zerolog" |
| 13 | + "log" |
| 14 | + "os" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +var errorFlag bool |
| 20 | + |
| 21 | +type OutputNotMatchingError struct{} |
| 22 | + |
| 23 | +func (e *OutputNotMatchingError) Error() string { |
| 24 | + return "Output does not match" |
| 25 | +} |
| 26 | + |
| 27 | +type MockAPI struct{} |
| 28 | + |
| 29 | +func (api MockAPI) UserPush(push abstraction.BrokerPush) error { |
| 30 | + switch push.(type) { |
| 31 | + case blcu.DownloadRequest: |
| 32 | + if push.(blcu.DownloadRequest).Board != "test" { |
| 33 | + errorFlag = true |
| 34 | + return &OutputNotMatchingError{} |
| 35 | + } |
| 36 | + errorFlag = false |
| 37 | + log.Printf("Output matches") |
| 38 | + return nil |
| 39 | + case blcu.UploadRequest: |
| 40 | + if push.(blcu.UploadRequest).Board != "test" || string(push.(blcu.UploadRequest).Data) != "test" { |
| 41 | + errorFlag = true |
| 42 | + fmt.Printf("Expected board 'test' and data 'test', got board '%s' and data '%s'\n", push.(blcu.UploadRequest).Board, string(push.(blcu.UploadRequest).Data)) |
| 43 | + return &OutputNotMatchingError{} |
| 44 | + } |
| 45 | + errorFlag = false |
| 46 | + log.Printf("Output matches") |
| 47 | + return nil |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (api MockAPI) UserPull(request abstraction.BrokerRequest) (abstraction.BrokerResponse, error) { |
| 53 | + return nil, nil |
| 54 | +} |
| 55 | + |
| 56 | +func TestBLCUTopic_Download_Push(t *testing.T) { |
| 57 | + logger := zerolog.New(os.Stdout).With().Timestamp().Logger() |
| 58 | + clientChan := make(chan *websocket.Client) |
| 59 | + u := tests_functions.StartServer(logger, "download") |
| 60 | + |
| 61 | + // Mock first client as it always fails |
| 62 | + c, _, err := ws.DefaultDialer.Dial(u.String(), nil) |
| 63 | + if err != nil { |
| 64 | + log.Printf("Expected dial error") |
| 65 | + } |
| 66 | + c.Close() |
| 67 | + |
| 68 | + // Set up the client |
| 69 | + c, _, err = ws.DefaultDialer.Dial(u.String(), nil) |
| 70 | + if err != nil { |
| 71 | + logger.Fatal().Err(err).Msg("Error dialing") |
| 72 | + } |
| 73 | + defer c.Close() |
| 74 | + defer logger.Info().Str("id", "client").Msg("Client connection closed") |
| 75 | + |
| 76 | + api := broker.New(logger) |
| 77 | + pool := websocket.NewPool(clientChan, logger) |
| 78 | + client := websocket.NewClient(c) |
| 79 | + clientChan <- client |
| 80 | + |
| 81 | + download := blcu.Download{} |
| 82 | + download.SetAPI(api) |
| 83 | + download.SetPool(pool) |
| 84 | + |
| 85 | + // Simulate sending a download request |
| 86 | + request := blcu.DownloadRequest{Board: "test"} |
| 87 | + err = download.Push(request) |
| 88 | + if err != nil { |
| 89 | + t.Fatal("Error pushing download request:", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Use a timeout for client read |
| 93 | + done := make(chan struct{}) |
| 94 | + go func() { |
| 95 | + output, readErr := client.Read() |
| 96 | + if readErr != nil { |
| 97 | + logger.Error().Err(readErr).Msg("Client read failed") |
| 98 | + done <- struct{}{} |
| 99 | + return |
| 100 | + } |
| 101 | + if output.Topic != blcu.DownloadName { |
| 102 | + t.Errorf("Expected topic %s, got %s", blcu.DownloadName, output.Topic) |
| 103 | + } |
| 104 | + if string(output.Payload) != "test" { |
| 105 | + t.Error("Expected payload 'test', got", string(output.Payload)) |
| 106 | + } |
| 107 | + done <- struct{}{} |
| 108 | + }() |
| 109 | + |
| 110 | + select { |
| 111 | + case <-done: |
| 112 | + logger.Info().Msg("Test completed successfully") |
| 113 | + case <-time.After(3 * time.Second): |
| 114 | + t.Error("Test timed out") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestBLCUTopic_Download_ClientMessage(t *testing.T) { |
| 119 | + download := blcu.Download{} |
| 120 | + download.SetAPI(&MockAPI{}) |
| 121 | + |
| 122 | + download.ClientMessage(websocket.ClientId{0}, &websocket.Message{ |
| 123 | + Topic: blcu.DownloadName, |
| 124 | + Payload: []byte(`{"board":"test"}`), |
| 125 | + }) |
| 126 | + |
| 127 | + if errorFlag { |
| 128 | + t.Fatal("Output does not match") |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestBLCUTopic_Upload_Push(t *testing.T) { |
| 133 | + logger := zerolog.New(os.Stdout).With().Timestamp().Logger() |
| 134 | + clientChan := make(chan *websocket.Client) |
| 135 | + u := tests_functions.StartServer(logger, "upload") |
| 136 | + |
| 137 | + // Set up the client |
| 138 | + c, _, err := ws.DefaultDialer.Dial(u.String(), nil) |
| 139 | + if err != nil { |
| 140 | + logger.Fatal().Err(err).Msg("Error dialing") |
| 141 | + } |
| 142 | + defer c.Close() |
| 143 | + defer logger.Info().Str("id", "client").Msg("Client connection closed") |
| 144 | + |
| 145 | + api := broker.New(logger) |
| 146 | + pool := websocket.NewPool(clientChan, logger) |
| 147 | + client := websocket.NewClient(c) |
| 148 | + clientChan <- client |
| 149 | + |
| 150 | + upload := blcu.Upload{} |
| 151 | + upload.SetAPI(api) |
| 152 | + upload.SetPool(pool) |
| 153 | + |
| 154 | + // Simulate sending a download request |
| 155 | + request := blcu.UploadRequest{Board: "test", Data: []byte("test")} |
| 156 | + err = upload.Push(request) |
| 157 | + if err != nil { |
| 158 | + t.Fatal("Error pushing upload request:", err) |
| 159 | + } |
| 160 | + |
| 161 | + // Use a timeout for client read |
| 162 | + done := make(chan struct{}) |
| 163 | + go func() { |
| 164 | + output, err := client.Read() |
| 165 | + if err != nil { |
| 166 | + logger.Error().Err(err).Msg("Client read failed") |
| 167 | + done <- struct{}{} |
| 168 | + return |
| 169 | + } |
| 170 | + if output.Topic != blcu.UploadName { |
| 171 | + t.Errorf("Expected topic %s, got %s", blcu.UploadName, output.Topic) |
| 172 | + } |
| 173 | + if string(output.Payload) != "test" { |
| 174 | + t.Error("Expected payload 'test', got", string(output.Payload)) |
| 175 | + } |
| 176 | + done <- struct{}{} |
| 177 | + }() |
| 178 | + |
| 179 | + select { |
| 180 | + case <-done: |
| 181 | + logger.Info().Msg("Test completed successfully") |
| 182 | + case <-time.After(3 * time.Second): |
| 183 | + t.Error("Test timed out") |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestBLCUTopic_Upload_ClientMessage(t *testing.T) { |
| 188 | + upload := blcu.Upload{} |
| 189 | + upload.SetAPI(&MockAPI{}) |
| 190 | + |
| 191 | + payload := blcu.UploadRequest{Board: "test", Data: []byte("test")} |
| 192 | + payloadBytes, _ := json.Marshal(payload) |
| 193 | + |
| 194 | + upload.ClientMessage(websocket.ClientId{0}, &websocket.Message{ |
| 195 | + Topic: blcu.UploadName, |
| 196 | + Payload: payloadBytes, |
| 197 | + }) |
| 198 | + |
| 199 | + if errorFlag { |
| 200 | + t.Fatal("Output does not match") |
| 201 | + } |
| 202 | +} |
0 commit comments