Skip to content

Commit f9841f5

Browse files
authored
Fixing TestOutgoingTrailerMatcher, which was non-deterministic (#4265)
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.
1 parent 17afee4 commit f9841f5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

runtime/handler_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"reflect"
9+
"sort"
910
"testing"
1011

1112
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -416,7 +417,7 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
416417
headers: http.Header{
417418
"Transfer-Encoding": []string{"chunked"},
418419
"Content-Type": []string{"application/json"},
419-
"Trailer": []string{"Grpc-Trailer-Foo", "Grpc-Trailer-Baz"},
420+
"Trailer": []string{"Grpc-Trailer-Baz", "Grpc-Trailer-Foo"},
420421
},
421422
trailer: http.Header{
422423
"Grpc-Trailer-Foo": []string{"bar"},
@@ -483,6 +484,9 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
483484
t.Fatalf("StatusCode %d want %d", w.StatusCode, http.StatusOK)
484485
}
485486

487+
// Sort to the trailer headers to ensure the test is deterministic
488+
sort.Strings(w.Header["Trailer"])
489+
486490
if !reflect.DeepEqual(w.Header, tc.headers) {
487491
t.Fatalf("Header %v want %v", w.Header, tc.headers)
488492
}

0 commit comments

Comments
 (0)