UNPKG

3.72 kBJavaScriptView Raw
1/**
2 * Test for TheClient.
3 * Runs with mocha.
4 */
5'use strict'
6
7const {TheServer} = require('the-server')
8const TheClient = require('../lib/TheClient')
9const {TheError} = require('the-error')
10const {ok, equal, deepEqual} = require('assert')
11const aport = require('aport')
12const fs = require('fs')
13const asleep = require('asleep')
14
15describe('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
136 onOpen () {
137 this.count = this.params.count
138 void this.countdown()
139 }
140
141 async countdown () {
142 while (this.count > 0) {
143 await this.send({count: this.count})
144 await asleep(1)
145 this.count--
146 }
147 this.close()
148 }
149 }
150
151 const server = new TheServer({
152 streams: {
153 countdown: CountdownStream
154 }
155 })
156 await server.listen(port)
157
158 const client01 = TheClient.for('c1', {port})
159
160 {
161 const stream = await client01.stream('countdown', {count: 100})
162 const received = []
163 stream.on('receive', (data) => received.push(data))
164 await stream.wait()
165 console.log(received)
166 }
167
168 await client01.close()
169 await server.close()
170 await asleep(100)
171 })
172})
173
174/* global describe, before, after, it */