| // |
| 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 registry |
| 16 |
| 17 | import ( |
| 18 | "testing" |
| 19 | "time" |
| 20 |
| 21 | clocktesting "k8s.io/utils/clock/testing" |
| 22 |
| 23 | "github.com/fatedier/frp/pkg/proto/wire" |
| 24 | ) |
| 25 |
| 26 | func TestClientRegistryRegisterStoresWireProtocol(t *testing.T) { |
| 27 | registry := NewClientRegistry() |
| 28 | key, conflict := registry.Register("user", "client-id", "run-id", "host", "1.0.0", "127.0.0.1", wire.ProtocolV2) |
| 29 | if conflict { |
| 30 | t.Fatal("unexpected client conflict") |
| 31 | } |
| 32 |
| 33 | info, ok := registry.GetByKey(key) |
| 34 | if !ok { |
| 35 | t.Fatalf("client %q not found", key) |
| 36 | } |
| 37 | if info.WireProtocol != wire.ProtocolV2 { |
| 38 | t.Fatalf("wire protocol mismatch, want %q got %q", wire.ProtocolV2, info.WireProtocol) |
| 39 | } |
| 40 | } |
| 41 |
| 42 | func TestClientRegistryUsesClockForTimestamps(t *testing.T) { |
| 43 | start := time.Date(2026, time.May, 8, 12, 30, 0, 0, time.UTC) |
| 44 | clk := clocktesting.NewFakeClock(start) |
| 45 | registry := newClientRegistryWithClock(clk) |
| 46 |
| 47 | key, conflict := registry.Register("user", "client-id", "run-id", "host", "1.0.0", "127.0.0.1", wire.ProtocolV2) |
| 48 | if conflict { |
| 49 | t.Fatal("unexpected client conflict") |
| 50 | } |
| 51 |
| 52 | info, ok := registry.GetByKey(key) |
| 53 | if !ok { |
| 54 | t.Fatalf("client %q not found", key) |
| 55 | } |
| 56 | if !info.FirstConnectedAt.Equal(start) { |
| 57 | t.Fatalf("first connected time mismatch, want %s got %s", start, info.FirstConnectedAt) |
| 58 | } |
| 59 | if !info.LastConnectedAt.Equal(start) { |
| 60 | t.Fatalf("last connected time mismatch, want %s got %s", start, info.LastConnectedAt) |
| 61 | } |
| 62 |
| 63 | disconnectedAt := start.Add(time.Minute) |
| 64 | clk.SetTime(disconnectedAt) |
| 65 | registry.MarkOfflineByRunID("run-id") |
| 66 |
| 67 | info, ok = registry.GetByKey(key) |
| 68 | if !ok { |
| 69 | t.Fatalf("client %q not found after disconnect", key) |
| 70 | } |
| 71 | if !info.DisconnectedAt.Equal(disconnectedAt) { |
| 72 | t.Fatalf("disconnected time mismatch, want %s got %s", disconnectedAt, info.DisconnectedAt) |
| 73 | } |
| 74 | } |