1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict'
|
6 |
|
7 | const { TheServer } = require('the-server')
|
8 | const TheClient = require('../lib/TheClient')
|
9 | const { TheError } = require('the-error')
|
10 | const { ok, equal, deepEqual } = require('assert')
|
11 | const aport = require('aport')
|
12 | const fs = require('fs')
|
13 | const asleep = require('asleep')
|
14 |
|
15 | describe('the-client', () => {
|
16 | before(() => {
|
17 | })
|
18 |
|
19 | after(() => {
|
20 | })
|
21 |
|
22 | it('Do test', async function () {
|
23 | this.timeout(20 * 1000)
|
24 | ok(TheClient)
|
25 |
|
26 | const port = await aport()
|
27 |
|
28 | class FruitShopCtrl extends TheServer.Ctrl {
|
29 |
|
30 | buy (name, amount) {
|
31 | const { session, callbacks } = this
|
32 | const { total = 0 } = session
|
33 | session.total = total + amount
|
34 | const result = { name, amount, total: session.total }
|
35 | asleep(10)
|
36 | callbacks.onBuy(amount, session.total)
|
37 | return result
|
38 | }
|
39 |
|
40 | doWrong () {
|
41 | throw new TheError('Something is wrong')
|
42 | }
|
43 | }
|
44 |
|
45 | const server = new TheServer({
|
46 | controllers: {
|
47 | fruitShop: FruitShopCtrl
|
48 | }
|
49 | })
|
50 | await server.listen(port)
|
51 |
|
52 | const onBuyData = []
|
53 |
|
54 | {
|
55 | const client01 = TheClient.for('c1', { port })
|
56 | const client02 = TheClient.for('c2', { port })
|
57 |
|
58 | ok(client01.cid)
|
59 | ok(client02.cid)
|
60 |
|
61 | const { fruitShop: fruitShop02 } = await client02.useAll()
|
62 | const fruitShop01 = await client01.use('fruitShop')
|
63 |
|
64 | fruitShop01.setCallback({
|
65 | onBuy (...results) {
|
66 | onBuyData.push(results)
|
67 | }
|
68 | })
|
69 |
|
70 | {
|
71 |
|
72 | const controllers = await client01.useAll()
|
73 | deepEqual(Object.keys(controllers), ['fruitShop'])
|
74 | ok(controllers['fruitShop'])
|
75 | }
|
76 |
|
77 | deepEqual(
|
78 | await fruitShop01.buy('orange', 100),
|
79 | { name: 'orange', amount: 100, total: 100 }
|
80 | )
|
81 |
|
82 | deepEqual(
|
83 | await fruitShop01.buy('orange', 400),
|
84 | { name: 'orange', amount: 400, total: 500 }
|
85 | )
|
86 |
|
87 | deepEqual(
|
88 | await fruitShop02.buy('orange', 400),
|
89 | { name: 'orange', amount: 400, total: 400 }
|
90 | )
|
91 |
|
92 | {
|
93 | const caught = await fruitShop01.doWrong().catch((e) => e)
|
94 | ok(caught)
|
95 | }
|
96 |
|
97 | await asleep(100)
|
98 |
|
99 | client01.pingPongAnd(() => console.log('pong!'), {
|
100 | interval: 10,
|
101 | retryMax: 3,
|
102 | })
|
103 |
|
104 | await asleep(100)
|
105 |
|
106 | const serverInfo = await client02.serverInfo()
|
107 | deepEqual(serverInfo.controllers, [
|
108 | {
|
109 | 'methods': {
|
110 | 'buy': { 'desc': 'buy' },
|
111 | 'doWrong': { 'desc': 'doWrong' }
|
112 | },
|
113 | 'name': 'fruitShop'
|
114 | }
|
115 | ])
|
116 | await client01.close()
|
117 |
|
118 | await asleep(10)
|
119 |
|
120 | await client02.close()
|
121 |
|
122 | }
|
123 |
|
124 | await server.close()
|
125 |
|
126 | await asleep(200)
|
127 |
|
128 | deepEqual(onBuyData, [[100, 100], [400, 500]])
|
129 | })
|
130 |
|
131 | it('Using stream api', async () => {
|
132 | const port = await aport()
|
133 |
|
134 | class CountdownStream extends TheServer.Stream {
|
135 | async * provide () {
|
136 | let count = Number(this.params.count)
|
137 | while (count > 0) {
|
138 | if (this.closed) {
|
139 | return
|
140 | }
|
141 | yield { count }
|
142 | count--
|
143 | await asleep(1)
|
144 | }
|
145 | }
|
146 | }
|
147 |
|
148 | const server = new TheServer({
|
149 | streams: {
|
150 | countdown: CountdownStream
|
151 | }
|
152 | })
|
153 | await server.listen(port)
|
154 |
|
155 | const client01 = TheClient.for('c1', { port })
|
156 |
|
157 | {
|
158 | const stream = await client01.stream('countdown', { count: 2 })
|
159 | equal((await stream.pull()).count, 2)
|
160 | equal((await stream.pull()).count, 1)
|
161 | await stream.close()
|
162 | }
|
163 |
|
164 | {
|
165 | const client02 = TheClient.for('c1', { port })
|
166 | const stream = await client01.stream('countdown', { count: 2 })
|
167 | void stream.pull()
|
168 | await client02.close()
|
169 | await stream.close()
|
170 | }
|
171 |
|
172 | await client01.close()
|
173 | await server.close()
|
174 | await asleep(100)
|
175 | })
|
176 |
|
177 | it('Stream client to server', async () => {
|
178 | const port = await aport()
|
179 | const consumed = []
|
180 |
|
181 | class CountupStream extends TheServer.Stream {
|
182 | async consume (provided) {
|
183 | for await (const chunk of provided) {
|
184 | consumed.push(chunk)
|
185 | }
|
186 | }
|
187 | }
|
188 |
|
189 | const server = new TheServer({
|
190 | streams: {
|
191 | countup: CountupStream,
|
192 | }
|
193 | })
|
194 | await server.listen(port)
|
195 |
|
196 | const client01 = TheClient.for('c1', { port })
|
197 | {
|
198 | const stream = await client01.stream('countup')
|
199 | await stream.push('hoge')
|
200 | await stream.push('hoge1')
|
201 | await stream.push('hoge2')
|
202 | await asleep(0)
|
203 | await stream.pushEnd()
|
204 | await asleep(100)
|
205 | equal(consumed.length, 3)
|
206 | await stream.close()
|
207 | }
|
208 | await asleep(100)
|
209 | await client01.close()
|
210 | await server.close()
|
211 | })
|
212 | })
|
213 |
|
214 |
|