Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
fbda6e2b
Commit
fbda6e2b
authored
May 05, 2019
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom consumer selector unit test
parent
3e25be3f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
152 additions
and
1 deletion
+152
-1
IConsumerServiceSelector.Default.cs
src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs
+1
-1
CustomConsumerSubscribeTest.cs
test/DotNetCore.CAP.Test/CustomConsumerSubscribeTest.cs
+151
-0
No files found.
src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs
View file @
fbda6e2b
...
...
@@ -23,7 +23,7 @@ namespace DotNetCore.CAP
private
readonly
IServiceProvider
_serviceProvider
;
/// <summary>
/// since this class be designed as a Singleton service,the following two list must be thread safe!
!!
/// since this class be designed as a Singleton service,the following two list must be thread safe!
/// </summary>
private
readonly
ConcurrentDictionary
<
string
,
List
<
RegexExecuteDescriptor
<
ConsumerExecutorDescriptor
>>>
_asteriskList
;
private
readonly
ConcurrentDictionary
<
string
,
List
<
RegexExecuteDescriptor
<
ConsumerExecutorDescriptor
>>>
_poundList
;
...
...
test/DotNetCore.CAP.Test/CustomConsumerSubscribeTest.cs
0 → 100644
View file @
fbda6e2b
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Reflection
;
using
System.Threading.Tasks
;
using
Microsoft.Extensions.DependencyInjection
;
using
Xunit
;
namespace
DotNetCore.CAP.Test
{
public
class
CustomConsumerSubscribeTest
{
private
readonly
IServiceProvider
_provider
;
public
CustomConsumerSubscribeTest
()
{
var
services
=
new
ServiceCollection
();
services
.
AddSingleton
<
IConsumerServiceSelector
,
MyConsumerServiceSelector
>();
services
.
AddTransient
<
IMySubscribe
,
CustomInterfaceTypesClass
>();
services
.
AddLogging
();
services
.
AddCap
(
x
=>
{
});
_provider
=
services
.
BuildServiceProvider
();
}
[
Fact
]
public
void
CanFindAllConsumerService
()
{
var
selector
=
_provider
.
GetRequiredService
<
IConsumerServiceSelector
>();
var
candidates
=
selector
.
SelectCandidates
();
Assert
.
Equal
(
2
,
candidates
.
Count
);
}
[
Fact
]
public
void
CanFindSpecifiedTopic
()
{
var
selector
=
_provider
.
GetRequiredService
<
IConsumerServiceSelector
>();
var
candidates
=
selector
.
SelectCandidates
();
var
bestCandidates
=
selector
.
SelectBestCandidate
(
"Candidates.Foo"
,
candidates
);
Assert
.
NotNull
(
bestCandidates
);
Assert
.
NotNull
(
bestCandidates
.
MethodInfo
);
Assert
.
Equal
(
typeof
(
Task
),
bestCandidates
.
MethodInfo
.
ReturnType
);
}
}
public
class
MyConsumerServiceSelector
:
DefaultConsumerServiceSelector
{
private
readonly
CapOptions
_capOptions
;
public
MyConsumerServiceSelector
(
IServiceProvider
serviceProvider
,
CapOptions
capOptions
)
:
base
(
serviceProvider
,
capOptions
)
{
_capOptions
=
capOptions
;
}
protected
override
IEnumerable
<
ConsumerExecutorDescriptor
>
FindConsumersFromInterfaceTypes
(
IServiceProvider
provider
)
{
var
executorDescriptorList
=
new
List
<
ConsumerExecutorDescriptor
>();
using
(
var
scoped
=
provider
.
CreateScope
())
{
var
scopedProvider
=
scoped
.
ServiceProvider
;
var
consumerServices
=
scopedProvider
.
GetServices
<
IMySubscribe
>();
foreach
(
var
service
in
consumerServices
)
{
var
typeInfo
=
service
.
GetType
().
GetTypeInfo
();
if
(!
typeof
(
IMySubscribe
).
GetTypeInfo
().
IsAssignableFrom
(
typeInfo
))
{
continue
;
}
executorDescriptorList
.
AddRange
(
GetMyDescription
(
typeInfo
));
}
return
executorDescriptorList
;
}
}
private
IEnumerable
<
ConsumerExecutorDescriptor
>
GetMyDescription
(
TypeInfo
typeInfo
)
{
foreach
(
var
method
in
typeInfo
.
DeclaredMethods
)
{
var
topicAttr
=
method
.
GetCustomAttributes
<
MySubscribeAttribute
>(
true
);
var
topicAttributes
=
topicAttr
as
IList
<
MySubscribeAttribute
>
??
topicAttr
.
ToList
();
if
(!
topicAttributes
.
Any
())
{
continue
;
}
foreach
(
var
attr
in
topicAttributes
)
{
if
(
attr
.
Group
==
null
)
{
attr
.
Group
=
_capOptions
.
DefaultGroup
+
"."
+
_capOptions
.
Version
;
}
else
{
attr
.
Group
=
attr
.
Group
+
"."
+
_capOptions
.
Version
;
}
yield
return
new
ConsumerExecutorDescriptor
{
Attribute
=
new
CapSubscribeAttribute
(
attr
.
Name
)
{
Group
=
attr
.
Group
},
MethodInfo
=
method
,
ImplTypeInfo
=
typeInfo
};
}
}
}
}
public
interface
IMySubscribe
{
}
public
class
MySubscribeAttribute
:
Attribute
{
public
MySubscribeAttribute
(
string
name
)
{
Name
=
name
;
}
public
string
Name
{
get
;
}
public
string
Group
{
get
;
set
;
}
}
public
class
CustomInterfaceTypesClass
:
IMySubscribe
{
[
MySubscribe
(
"Candidates.Foo"
)]
public
Task
GetFoo
()
{
Console
.
WriteLine
(
"GetFoo() method has been excuted."
);
return
Task
.
CompletedTask
;
}
[
MySubscribe
(
"Candidates.Foo2"
)]
public
void
GetFoo2
()
{
Console
.
WriteLine
(
"GetFoo2() method has been excuted."
);
}
public
void
DistracterMethod
()
{
Console
.
WriteLine
(
"DistracterMethod() method has been excuted."
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment