|
| 1 | +// Copyright © 2026 The Things Network Foundation, The Things Industries B.V. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package semtechws |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/gorilla/websocket" |
| 23 | + "github.com/smarty/assertions" |
| 24 | + "go.thethings.network/lorawan-stack/v3/pkg/errors" |
| 25 | + "go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDisconnectError(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + for _, tc := range []struct { |
| 32 | + Name string |
| 33 | + Err error |
| 34 | + // Code is the expected close code, nil if the error is expected to pass through. |
| 35 | + Code any |
| 36 | + }{ |
| 37 | + { |
| 38 | + // This is what the gateway disappearing without a close handshake looks like. |
| 39 | + Name: "AbnormalClosure", |
| 40 | + Err: &websocket.CloseError{Code: websocket.CloseAbnormalClosure, Text: io.ErrUnexpectedEOF.Error()}, |
| 41 | + Code: websocket.CloseAbnormalClosure, |
| 42 | + }, |
| 43 | + { |
| 44 | + Name: "GoingAway", |
| 45 | + Err: &websocket.CloseError{Code: websocket.CloseGoingAway}, |
| 46 | + Code: websocket.CloseGoingAway, |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: "WrappedCloseError", |
| 50 | + Err: fmt.Errorf("read: %w", |
| 51 | + &websocket.CloseError{Code: websocket.CloseNoStatusReceived}, |
| 52 | + ), |
| 53 | + Code: websocket.CloseNoStatusReceived, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "DefinedError", |
| 57 | + Err: errMissedTooManyPongs.New(), |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "OtherError", |
| 61 | + Err: io.ErrUnexpectedEOF, |
| 62 | + }, |
| 63 | + } { |
| 64 | + t.Run(tc.Name, func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + a := assertions.New(t) |
| 67 | + |
| 68 | + err := disconnectError(tc.Err) |
| 69 | + if tc.Code == nil { |
| 70 | + a.So(err, should.Equal, tc.Err) |
| 71 | + return |
| 72 | + } |
| 73 | + if !a.So(errors.IsAborted(err), should.BeTrue) { |
| 74 | + t.FailNow() |
| 75 | + } |
| 76 | + ttnErr, ok := errors.From(err) |
| 77 | + if !a.So(ok, should.BeTrue) { |
| 78 | + t.FailNow() |
| 79 | + } |
| 80 | + // The error must be classifiable, so that it can be used as a metric label. |
| 81 | + a.So(ttnErr.FullName(), should.Equal, "pkg/gatewayserver/io/semtechws:websocket_closed") |
| 82 | + a.So(ttnErr.Attributes()["code"], should.Equal, tc.Code) |
| 83 | + // The original error must be preserved for diagnostics. |
| 84 | + a.So(errors.Is(err, tc.Err), should.BeTrue) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments