Files
vm/api/server/metrics_test.go
zeekayandHanzo Dev e9e9fb6315 Give the node's HTTP server a home beside the VMs it serves
api/server was an earlier copy of node/server/http that had drifted behind it.
Its exported surface was a strict subset of the node's, so this replaces the
contents rather than adding a second package: a second server would have meant
two Server interfaces, two New, two routers, and no answer to which one a chain
should mount on.

What arrives with it is the part api/server never had. Mount is the estate's
only projection from a zip App to an http.Handler, and it installs Authorize
before it serves, so an app cannot reach the router without the node's
authorization decision on it. Chain builds the one address a chain answers at,
and chain_test walks this module's own source to hold every caller to it. The
ZAP-RPC listener and the /v1 prefix come across as well; api/server was still
serving /ext.

Two files stay behind because they already have homes here. The node's
traced_handler duplicates api.TraceHandler, which this package was already
calling, so server.go keeps calling it and takes luxfi/trace in place of the
node-local trace shim.

Two tests do not come across, and neither is dropped from the node. mount_test
builds a real Z-Chain from luxfi/chains, which requires luxfi/vm, so importing
it here would point the layering back at itself. probe_test needs the Ops app
on node/service/health, which api/health does not carry. Both keep running
where they are.

constants moves to v1.6.4 to agree with the node on the chain segment. The two
modules disagreed, v1.6.2 still saying bc where the node says chain, and the
address Chain builds has to be the address the node's router serves.

newMetrics now reports a duplicate registration instead of returning a
half-built set, which is what TestMetricsRegistrationFailure had been asking
for and failing to get. That test stays, next to the node's, because the two
ask different questions: one registry twice, or two registries once.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-06 12:21:19 -07:00

101 lines
2.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package server
import (
"testing"
"github.com/luxfi/metric"
"github.com/stretchr/testify/require"
)
func TestNewMetrics(t *testing.T) {
// Create a new registry for testing
reg := metric.NewRegistry()
// Create metrics
metrics, err := newMetrics(reg)
require.NoError(t, err)
require.NotNil(t, metrics)
require.NotNil(t, metrics.requests)
require.NotNil(t, metrics.duration)
require.NotNil(t, metrics.inflight)
// Test basic operations to ensure they work
metrics.requests.WithLabelValues("GET", "/test").Inc()
metrics.duration.WithLabelValues("POST", "/api").Observe(0.5)
metrics.inflight.Inc()
metrics.inflight.Dec()
}
// TestMetricsMultipleRegistrations verifies that metrics can be created
// on separate registries without conflict.
func TestMetricsMultipleRegistrations(t *testing.T) {
reg1 := metric.NewRegistry()
reg2 := metric.NewRegistry()
// First registration should succeed
metrics1, err := newMetrics(reg1)
require.NoError(t, err)
require.NotNil(t, metrics1)
// Second registration on different registry should also succeed
metrics2, err := newMetrics(reg2)
require.NoError(t, err)
require.NotNil(t, metrics2)
// Both should be usable
metrics1.requests.WithLabelValues("GET", "/test").Inc()
metrics2.requests.WithLabelValues("POST", "/test").Inc()
}
func TestMetricsOperations(t *testing.T) {
reg := metric.NewRegistry()
metrics, err := newMetrics(reg)
require.NoError(t, err)
// Test various label combinations
testCases := []struct {
method string
endpoint string
duration float64
}{
{"GET", "/health", 0.001},
{"POST", "/api/v1/users", 0.123},
{"PUT", "/api/v1/users/123", 0.456},
{"DELETE", "/api/v1/users/456", 0.789},
{"GET", "/metrics", 0.002},
}
for _, tc := range testCases {
// Increment request counter
metrics.requests.WithLabelValues(tc.method, tc.endpoint).Inc()
// Observe duration
metrics.duration.WithLabelValues(tc.method, tc.endpoint).Observe(tc.duration)
// Simulate inflight request
metrics.inflight.Inc()
metrics.inflight.Dec()
}
// Operations completed successfully without panics
}
// TestMetricsRegistrationFailure pins the other half of the question
// TestMetricsMultipleRegistrations asks: two registrations on the SAME registry
// collide, and newMetrics reports it rather than returning a half-built set.
func TestMetricsRegistrationFailure(t *testing.T) {
reg := metric.NewRegistry()
metrics1, err := newMetrics(reg)
require.NoError(t, err)
require.NotNil(t, metrics1)
metrics2, err := newMetrics(reg)
require.Error(t, err, "second registration on the same registry should fail as a duplicate")
require.Nil(t, metrics2)
}