public
Description:
Homepage: http://www.iserviceoriented.com
Clone URL: git://github.com/jezell/iserviceoriented.git
100644 145 lines (105 sloc) 5.083 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.ServiceModel;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using IServiceOriented.ServiceBus.Dispatchers;
using System.ServiceModel.Channels;
using System.Threading;
using System.Xml;
using IServiceOriented.ServiceBus.Delivery;
 
namespace IServiceOriented.ServiceBus.UnitTests
{
[TestFixture]
    public class TestWcfDispatcher
    {
        public TestWcfDispatcher()
        {
        }
 
[Test]
        public void Can_Dispatch_To_ServiceHost()
        {
            ContractImplementation ci = new ContractImplementation();
            ServiceHost host = new ServiceHost(ci);
            host.Open();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore()))
            {
                WcfProxyDispatcher contractDispatcher = new WcfProxyDispatcher();
 
                SubscriptionEndpoint endpoint = new SubscriptionEndpoint(Guid.NewGuid(), "test", "NamedPipeClient", "net.pipe://localhost/remotehello", typeof(IContract), contractDispatcher, null);
 
                runtime.Subscribe(endpoint);
 
                runtime.Start();
 
 
                string message = "blah blah test test";
 
 
                contractDispatcher.Dispatch(new MessageDelivery(endpoint.Id, typeof(IContract), "PublishThis", message, 3, new MessageDeliveryContext()));
 
                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);
 
                runtime.Stop();
                host.Close();
            }
        }
 
[Test]
        public void Can_Dispatch_Raw_Messages_To_Pass_Through_Endpoint()
        {
            PassThroughService pts = new PassThroughService();
            ServiceHost host = new ServiceHost(pts);
            host.Open();
 
            WcfProxyDispatcher contractDispatcher = new WcfProxyDispatcher();
            SubscriptionEndpoint endpoint = new SubscriptionEndpoint(Guid.NewGuid(), "test", "PassThroughClient", "net.pipe://localhost/passthrough", typeof(IPassThroughServiceContract), contractDispatcher, new PassThroughMessageFilter());
 
            string action = "http://someaction";
            string body = "this is a test";
 
            pts.Validator = (msg) => { Assert.AreEqual(msg.Headers.Action, action); Assert.AreEqual(msg.GetBody<string>(), body); };
 
            Message message = Message.CreateMessage(MessageVersion.Default, action, body);
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore()))
            {
                runtime.Subscribe(endpoint);
 
                runtime.Start();
 
                contractDispatcher.Dispatch(new MessageDelivery(endpoint.Id, typeof(IPassThroughServiceContract), action, message, 3, new MessageDeliveryContext()));
 
                runtime.Stop();
            }
 
            Assert.AreEqual(1, pts.PublishedCount);
 
            host.Close();
        }
 
[Test]
        public void Can_Dispatch_Raw_Messages_To_Typed_Endpoint()
        {
            ContractImplementation ci = new ContractImplementation();
            ServiceHost host = new ServiceHost(ci);
            host.Open();
 
            WcfProxyDispatcher contractDispatcher = new WcfProxyDispatcher();
             
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore()))
            {
 
                SubscriptionEndpoint endpoint = new SubscriptionEndpoint(Guid.NewGuid(), "test", "PassThroughClient", "net.pipe://localhost/remotehello", typeof(IPassThroughServiceContract), contractDispatcher, null);
 
                runtime.Subscribe(endpoint);
 
                runtime.Start();
 
                string action = "PublishThis";
                string body = "blah blah test test";
 
                XmlDocument document = new XmlDocument();
                document.LoadXml("<PublishThis xmlns='http://tempuri.org/'><message>" + body + "</message></PublishThis>");
                Message message = Message.CreateMessage(MessageVersion.Default, action, new XmlNodeReader(document));
                contractDispatcher.Dispatch(new MessageDelivery(endpoint.Id, typeof(IPassThroughServiceContract), action, message, 3, new MessageDeliveryContext()));
 
                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(body, ci.PublishedMessages[0]);
 
                runtime.Stop();
            }
            
            host.Close();
        }
    }
 
[ServiceBehavior(ConfigurationName="PassThroughListener", InstanceContextMode=InstanceContextMode.Single)]
    public class PassThroughService : IPassThroughServiceContract
    {
        public void Send(Message message)
        {
            if (Validator != null) Validator(message);
            Interlocked.Increment(ref _count);
        }
 
        public Action<Message> Validator;
 
        int _count;
 
        public int PublishedCount
        {
            get
            {
                return _count;
            }
        }
    }
}