Skip to content

Commit

Permalink
Fixing TestOutgoingTrailerMatcher, which was non-deterministic (#4265)
Browse files Browse the repository at this point in the history
Recently the test was updated to also check the headers.  The test will sometimes fail when checking the headers returned, when there were multiple pairs added to the TrailerMD.  This is caused by the TrailerMD being a map, which has a non-deterministic read order.  The simple fix is to just sorts the Trailer headers returned before comparing.
  • Loading branch information
joshgarnett committed Apr 30, 2024
1 parent 17afee4 commit f9841f5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion runtime/handler_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"sort"
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -416,7 +417,7 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
headers: http.Header{
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Grpc-Trailer-Foo", "Grpc-Trailer-Baz"},
"Trailer": []string{"Grpc-Trailer-Baz", "Grpc-Trailer-Foo"},
},
trailer: http.Header{
"Grpc-Trailer-Foo": []string{"bar"},
Expand Down Expand Up @@ -483,6 +484,9 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
t.Fatalf("StatusCode %d want %d", w.StatusCode, http.StatusOK)
}

// Sort to the trailer headers to ensure the test is deterministic
sort.Strings(w.Header["Trailer"])

if !reflect.DeepEqual(w.Header, tc.headers) {
t.Fatalf("Header %v want %v", w.Header, tc.headers)
}
Expand Down

0 comments on commit f9841f5

Please sign in to comment.