Пример #1
0
        public void GetMethod()
        {
            var features = new Mock <IFeatureCollection>(MockBehavior.Strict);

            features
            .Setup(f => f.Get <IEndpointFeature>())
            .Returns((IEndpointFeature)null !);

            var context = new Mock <HttpContext>(MockBehavior.Strict);

            context
            .SetupGet(c => c.Features)
            .Returns(features.Object);

            _sut.GetMethod(context.Object).ShouldBeNull();

            var feature = new Mock <IEndpointFeature>(MockBehavior.Strict);

            feature
            .SetupGet(f => f.Endpoint)
            .Returns((Endpoint)null !);

            features
            .Setup(f => f.Get <IEndpointFeature>())
            .Returns(feature.Object);

            _sut.GetMethod(context.Object).ShouldBeNull();

            var endpoint = new Endpoint(null !, new EndpointMetadataCollection(), "s");

            feature
            .SetupGet(f => f.Endpoint)
            .Returns(endpoint);

            _sut.GetMethod(context.Object).ShouldBeNull();

            var method   = new Mock <IMethod>(MockBehavior.Strict);
            var metadata = new GrpcMethodMetadata(typeof(IDisposable), method.Object);

            endpoint = new Endpoint(null !, new EndpointMetadataCollection(metadata), "s");

            feature
            .SetupGet(f => f.Endpoint)
            .Returns(endpoint);

            _sut.GetMethod(context.Object).ShouldBe(method.Object);
        }
Пример #2
0
        public static void SetupHttpContext(DefaultHttpContext context, string expectedService,
                                            string expectedMethod)
        {
            var method = Substitute.For <IMethod>();

            method.ServiceName.Returns(expectedService);
            method.Name.Returns(expectedMethod);

            var metadata = new GrpcMethodMetadata(typeof(TestHelpers), method);

            var endpoint = new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(metadata), "gRPC");

            var endpointFeature = Substitute.For <IEndpointFeature>();

            endpointFeature.Endpoint.Returns(endpoint);

            context.Features[typeof(IEndpointFeature)] = endpointFeature;
        }
Пример #3
0
        protected TChild CreateChild(HttpContext context, GrpcMethodMetadata metadata)
        {
            var labelValues = new string[_metric.LabelNames.Length];

            for (var i = 0; i < labelValues.Length; i++)
            {
                switch (_metric.LabelNames[i])
                {
                case GrpcRequestLabelNames.Service:
                    labelValues[i] = metadata.Method.ServiceName;
                    break;

                case GrpcRequestLabelNames.Method:
                    labelValues[i] = metadata.Method.Name;
                    break;

                default:
                    throw new NotSupportedException($"Unexpected label name on {_metric.Name}: {_metric.LabelNames[i]}");
                }
            }

            return(_metric.WithLabels(labelValues));
        }