Skip to content

Commit 84e106e

Browse files
authored
add tests and fix for #87 (#97)
1 parent 5c8c102 commit 84e106e

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

docs/releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## (unreleased)
44

55
- support `CancellationToken` in service signatures in place of `CallContext` (#95)
6+
- service discovery correctly considers method accessibility (#87)
67

78
## 1.0.75
89

src/protobuf-net.Grpc/Internal/ContractOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static Type GetDataType((Type type, TypeCategory category) key, bool req)
265265
}
266266
public static List<ContractOperation> FindOperations(BinderConfiguration binderConfig, Type contractType, IBindContext? bindContext)
267267
{
268-
var all = contractType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
268+
var all = contractType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
269269
var ops = new List<ContractOperation>(all.Length);
270270
foreach (var method in all)
271271
{
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using Grpc.Core;
2+
using ProtoBuf.Grpc.Configuration;
3+
using System;
4+
using System.Collections.Generic;
5+
using Xunit;
6+
7+
namespace protobuf_net.Grpc.Test.Issues
8+
{
9+
class TestServerBinder : ServerBinder // just tracks what methods are observed
10+
{
11+
public HashSet<string> Methods { get; } = new HashSet<string>();
12+
public List<string> Warnings { get; } = new List<string>();
13+
public List<string> Errors { get; } = new List<string>();
14+
protected override bool TryBind<TService, TRequest, TResponse>(ServiceBindContext bindContext, Method<TRequest, TResponse> method, MethodStub<TService> stub)
15+
{
16+
Methods.Add(method.Name);
17+
return true;
18+
}
19+
protected internal override void OnWarn(string message, object?[]? args = null)
20+
=> Warnings.Add(string.Format(message, args));
21+
protected internal override void OnError(string message, object?[]? args = null)
22+
=> Errors.Add(string.Format(message, args));
23+
}
24+
25+
public class Issue87
26+
{
27+
[Theory]
28+
[InlineData(typeof(MyService), null)]
29+
[InlineData(typeof(MyServiceBase), null)]
30+
[InlineData(typeof(Foo), nameof(Foo))]
31+
[InlineData(typeof(FooBase), null)]
32+
[InlineData(typeof(Bar), nameof(Bar))]
33+
[InlineData(typeof(BarBase), nameof(BarBase))]
34+
[InlineData(typeof(IDerivedService), "protobuf_net.Grpc.Test.Issues.DerivedService")]
35+
[InlineData(typeof(IBaseService), "protobuf_net.Grpc.Test.Issues.BaseService")]
36+
[InlineData(typeof(INotDerivedService), null)]
37+
[InlineData(typeof(INotBaseService), null)]
38+
public void IsService(Type type, string name)
39+
{
40+
if (string.IsNullOrWhiteSpace(name))
41+
{
42+
Assert.False(ServiceBinder.Default.IsServiceContract(type, out _));
43+
Assert.Null(name);
44+
}
45+
else
46+
{
47+
Assert.True(ServiceBinder.Default.IsServiceContract(type, out var svcName));
48+
Assert.Equal(name, svcName);
49+
}
50+
}
51+
52+
[Theory]
53+
[InlineData(typeof(Foo), new[] { nameof(Foo.PublicDerived), nameof(FooBase.PublicBase), nameof(FooBase.PublicPolymorphic) })]
54+
[InlineData(typeof(FooBase), new string[] { })]
55+
[InlineData(typeof(Bar), new[] { nameof(Bar.PublicDerived), nameof(BarBase.PublicBase), nameof(BarBase.PublicPolymorphic) })]
56+
[InlineData(typeof(BarBase), new[] { nameof(BarBase.PublicBase), nameof(BarBase.PublicPolymorphic) })]
57+
[InlineData(typeof(MyServiceBase), new[] { nameof(IBaseService.BaseServiceMethodExplicit), nameof(IBaseService.BaseServiceMethodImplicit) })]
58+
[InlineData(typeof(MyService), new[] { nameof(IBaseService.BaseServiceMethodExplicit), nameof(IBaseService.BaseServiceMethodImplicit), nameof(IDerivedService.DerivedServiceMethodExplicit), nameof(IDerivedService.DerivedServiceMethodImplicit) })]
59+
public void CanSeeCorrectMethods(Type type, string[] methods)
60+
{
61+
var binder = new TestServerBinder();
62+
int count = binder.Bind(this, type);
63+
Assert.Equal(methods.Length, count);
64+
Assert.Equal(methods.Length, binder.Methods.Count);
65+
foreach (var method in methods)
66+
{
67+
Assert.Contains(method, binder.Methods);
68+
}
69+
70+
Assert.Empty(binder.Warnings);
71+
Assert.Empty(binder.Errors);
72+
}
73+
74+
class MyService : MyServiceBase, IDerivedService, INotDerivedService
75+
{
76+
public void DerivedServiceMethodImplicit() { }
77+
public void DerivedNotServiceMethodImplicit() { }
78+
79+
void IDerivedService.DerivedServiceMethodExplicit() { }
80+
void INotDerivedService.DerivedNotServiceMethodExplicit() { }
81+
}
82+
83+
class MyServiceBase : IBaseService, INotBaseService
84+
{
85+
public void BaseNotServiceMethodImplicit() { }
86+
public void BaseServiceMethodImplicit() { }
87+
88+
void IBaseService.BaseServiceMethodExplicit() { }
89+
void INotBaseService.BaseNotServiceMethodExplicit() { }
90+
}
91+
92+
[Service]
93+
interface IDerivedService
94+
{
95+
void DerivedServiceMethodExplicit();
96+
void DerivedServiceMethodImplicit();
97+
}
98+
interface INotDerivedService
99+
{
100+
void DerivedNotServiceMethodExplicit();
101+
void DerivedNotServiceMethodImplicit();
102+
}
103+
[Service]
104+
interface IBaseService
105+
{
106+
void BaseServiceMethodExplicit();
107+
void BaseServiceMethodImplicit();
108+
}
109+
interface INotBaseService
110+
{
111+
void BaseNotServiceMethodExplicit();
112+
void BaseNotServiceMethodImplicit();
113+
}
114+
115+
class Foo : FooBase, IGrpcService
116+
{
117+
public void PublicDerived() { }
118+
internal void NonPublicDerived() { }
119+
protected void ProtectedDerived() { }
120+
121+
public override void PublicPolymorphic() { }
122+
protected override void ProtectedPolymorphic() { }
123+
}
124+
class FooBase
125+
{
126+
public void PublicBase() { }
127+
internal void NonPublicBase() { }
128+
protected void ProtectedBase() { }
129+
130+
public virtual void PublicPolymorphic() { }
131+
protected virtual void ProtectedPolymorphic() { }
132+
}
133+
134+
public class Bar : BarBase
135+
{
136+
public void PublicDerived() { }
137+
internal void NonPublicDerived() { }
138+
protected void ProtectedDerived() { }
139+
140+
public override void PublicPolymorphic() { }
141+
protected override void ProtectedPolymorphic() { }
142+
}
143+
public class BarBase : IGrpcService
144+
{
145+
public void PublicBase() { }
146+
internal void NonPublicBase() { }
147+
protected void ProtectedBase() { }
148+
149+
public virtual void PublicPolymorphic() { }
150+
protected virtual void ProtectedPolymorphic() { }
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)