|
@@ -1,4 +1,4 @@
|
|
|
-import { Subject, take } from 'rxjs';
|
|
|
+import { Subject, from, take } from 'rxjs';
|
|
|
import { Message, MessageLog, ConnectionRequest } from '../interfaces/general.interface';
|
|
|
import { GrpcServiceMethod } from '../services/grpc.service.method';
|
|
|
import { readFileSync } from 'fs';
|
|
@@ -11,40 +11,49 @@ let parsedMessages: any[] = JSON.parse(messagesJSON) // load the fake messages g
|
|
|
let targetserver: string = 'localhost:3000'
|
|
|
let targetserver2: string = 'localhost:3002'
|
|
|
let hostServer: string = 'localhost:3001'
|
|
|
-let array: any[] = [] // Used for testing
|
|
|
+let array: any[] = [] // Used for testing
|
|
|
+let intervalToStreamOutGoingMessage: number = 1
|
|
|
+
|
|
|
+
|
|
|
+/* Simple Test: 1 to 1 */
|
|
|
let connectionRequest: ConnectionRequest = {
|
|
|
- database: 'grpc2',
|
|
|
server: {
|
|
|
+ name: 'g2',
|
|
|
serverUrl: hostServer,
|
|
|
connectionType: 'GRPC',
|
|
|
messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
},
|
|
|
client: {
|
|
|
+ name: 'g1',
|
|
|
targetServer: targetserver,
|
|
|
connectionType: 'GRPC',
|
|
|
messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Handler for the incoming Messages from the other side.
|
|
|
+connectionService.generateConnection(connectionRequest)
|
|
|
+// 10000th message == 848438e1-da50-4d98-aa12-e44d6d6a1489
|
|
|
+
|
|
|
+// let generateFakeMessagesToBePublished = stream().pipe(take(1000))
|
|
|
+let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(20000))
|
|
|
+generateFakeMessagesToBePublished.subscribe({
|
|
|
+ next: message => {
|
|
|
+ let payload: Message = {
|
|
|
+ id: hostServer,
|
|
|
+ message: message
|
|
|
+ }
|
|
|
+ // connectionRequest.server.messageToBePublishedfromApplication.next(payload)
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
- next: request => {
|
|
|
- // Application logic comes here. This is where the asortment takes place, of decidiing whose messages it belongs of what it is
|
|
|
- if ((request.message as MessageLog).appData.msgPayload == 'Query') {
|
|
|
- generateFakeStreamResponse(request).subscribe({
|
|
|
- next: (responseMessage: Message) => {
|
|
|
- // console.log(`Processing request:${request.id}....`)
|
|
|
- connectionRequest.server.messageToBePublishedfromApplication.next(responseMessage)
|
|
|
- },
|
|
|
- error: error => console.error(error),
|
|
|
- complete: () => {
|
|
|
- console.log(`Stream request for ${request.id} is queued.`) // shpuld be indefinite
|
|
|
- }
|
|
|
- })
|
|
|
- } else {
|
|
|
- array.push(request)
|
|
|
- console.log(`Received messages from the other side: ${(request.message as MessageLog).appData.msgId}`)
|
|
|
+ next: response => {
|
|
|
+ if((response.message as MessageLog).appData.msgId == `ebf94479-44fe-470d-827c-9f1389396d6a`){
|
|
|
+ console.log(`Received the 1000th message. Running the test. Initiating server restart....`)
|
|
|
+ connectionService.restartServerInDuration(10)
|
|
|
}
|
|
|
+ console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+ array.push(response)
|
|
|
},
|
|
|
error: error => console.error(error),
|
|
|
complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
@@ -52,9 +61,98 @@ connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
|
|
|
|
|
|
|
|
|
-connectionService.generateConnection(connectionRequest)
|
|
|
+/* Complex Test: 1 to 1*/
|
|
|
+// let connectionRequest: ConnectionRequest = {
|
|
|
+// server: {
|
|
|
+// name: 'g1',
|
|
|
+// serverUrl: hostServer,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
+// },
|
|
|
+// client: {
|
|
|
+// name: 'g2',
|
|
|
+// targetServer: targetserver,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// connectionService.generateConnection(connectionRequest)
|
|
|
+
|
|
|
+// setTimeout(() => {
|
|
|
+// let message = {
|
|
|
+// id: parsedMessages[10].appData.msgId,
|
|
|
+// message: parsedMessages[10] // Choose this number, because i purposely use the 11th message and change the msgPayload property to query to emulate a request
|
|
|
+// }
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// }, 3000)
|
|
|
+// setTimeout(() => {
|
|
|
+// let message = {
|
|
|
+// id: parsedMessages[11].appData.msgId,
|
|
|
+// message: parsedMessages[11] // Choose this number, because i purposely use the 12th message and change the msgPayload property to query to emulate a request
|
|
|
+// }
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// }, 4000)
|
|
|
+
|
|
|
+// Handler for the incoming Messages from the other side.
|
|
|
+// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// // Application logic comes here. This is where the asortment takes place, of decidiing whose messages it belongs of what it is
|
|
|
+// if ((request.message as MessageLog).appData.msgPayload == 'Query') {
|
|
|
+// generateFakeStreamResponse(request).subscribe({
|
|
|
+// next: (responseMessage: Message) => {
|
|
|
+// console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(responseMessage)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => {
|
|
|
+// console.log(`Stream request for ${request.id} is queued.`) // shpuld be indefinite
|
|
|
+// }
|
|
|
+// })
|
|
|
+// } else {
|
|
|
+// array.push(request)
|
|
|
+// console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
|
|
|
+// }
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/* Simple Test: 1 to Many */
|
|
|
+// let connectionRequest: ConnectionRequest = {
|
|
|
+// server: {
|
|
|
+// name: 'g1',
|
|
|
+// serverUrl: hostServer,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
+// },
|
|
|
+// client: {
|
|
|
+// name: 'g2',
|
|
|
+// targetServer: targetserver,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// let connectionRequest2: ConnectionRequest = {
|
|
|
+// server: {
|
|
|
+// name: 'g1',
|
|
|
+// serverUrl: hostServer,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
+// },
|
|
|
+// client: {
|
|
|
+// name: 'g3',
|
|
|
+// targetServer: targetserver2,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// connectionService.generateConnection(connectionRequest)
|
|
|
+// connectionService.generateConnection(connectionRequest2)
|
|
|
|
|
|
-/* Simple Test */
|
|
|
// let generateFakeMessagesToBePublished = stream().pipe(take(10))
|
|
|
// generateFakeMessagesToBePublished.subscribe({
|
|
|
// next: message => {
|
|
@@ -65,29 +163,136 @@ connectionService.generateConnection(connectionRequest)
|
|
|
// connectionRequest.server.messageToBePublishedfromApplication.next(payload)
|
|
|
// }
|
|
|
// })
|
|
|
+// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+
|
|
|
+// array.push(request)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
+
|
|
|
+// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+// array.push(request)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
+
|
|
|
+// connectionRequest2.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// array.push(request)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/* Complex Test: 1 to Many */
|
|
|
+// let connectionRequest: ConnectionRequest = {
|
|
|
+// server: {
|
|
|
+// name: 'g1',
|
|
|
+// serverUrl: hostServer,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
+// },
|
|
|
+// client: {
|
|
|
+// name: 'g2',
|
|
|
+// targetServer: targetserver,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// let connectionRequest2: ConnectionRequest = {
|
|
|
+// server: {
|
|
|
+// name: 'g1',
|
|
|
+// serverUrl: hostServer,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBePublishedfromApplication: new Subject<Message>()
|
|
|
+// },
|
|
|
+// client: {
|
|
|
+// name: 'g3',
|
|
|
+// targetServer: targetserver2,
|
|
|
+// connectionType: 'GRPC',
|
|
|
+// messageToBeReceivedFromRemote: new Subject<Message>()
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// connectionService.generateConnection(connectionRequest)
|
|
|
+// connectionService.generateConnection(connectionRequest2)
|
|
|
+
|
|
|
+// setTimeout(() => {
|
|
|
+// let message = {
|
|
|
+// id: parsedMessages[10].appData.msgId,
|
|
|
+// message: parsedMessages[10] // Choose this number, because i purposely use the 11th message and change the msgPayload property to query to emulate a request
|
|
|
+// }
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// connectionRequest2.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// }, 3000)
|
|
|
+// setTimeout(() => {
|
|
|
+// let message = {
|
|
|
+// id: parsedMessages[11].appData.msgId,
|
|
|
+// message: parsedMessages[11] // Choose this number, because i purposely use the 12th message and change the msgPayload property to query to emulate a request
|
|
|
+// }
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// connectionRequest2.server.messageToBePublishedfromApplication.next(message)
|
|
|
+// }, 4000)
|
|
|
+
|
|
|
+// connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// // Application logic comes here. This is where the asortment takes place, of deciding whose messages it belongs of what it is
|
|
|
+// if ((request.message as MessageLog).appData.msgPayload == 'Query') {
|
|
|
+// generateFakeStreamResponse(request).subscribe({
|
|
|
+// next: (responseMessage: Message) => {
|
|
|
+// connectionRequest.server.messageToBePublishedfromApplication.next(responseMessage)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => {
|
|
|
+// console.log(`Stream request for ${request.id} is queued.`) // should be indefinite
|
|
|
+// }
|
|
|
+// })
|
|
|
+// } else {
|
|
|
+// console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+
|
|
|
+// array.push(request)
|
|
|
+// console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
|
|
|
+// }
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
+// connectionRequest2.client.messageToBeReceivedFromRemote.subscribe({
|
|
|
+// next: request => {
|
|
|
+// // Application logic comes here. This is where the asortment takes place, of deciding whose messages it belongs of what it is
|
|
|
+// if ((request.message as MessageLog).appData.msgPayload == 'Query') {
|
|
|
+// generateFakeStreamResponse(request).subscribe({
|
|
|
+// next: (responseMessage: Message) => {
|
|
|
+// connectionRequest2.server.messageToBePublishedfromApplication.next(responseMessage)
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => {
|
|
|
+// console.log(`Stream request for ${request.id} is queued.`) // should be indefinite
|
|
|
+// }
|
|
|
+// })
|
|
|
+// } else {
|
|
|
+// console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
|
|
|
+
|
|
|
+// array.push(request)
|
|
|
+// console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
|
|
|
+// }
|
|
|
+// },
|
|
|
+// error: error => console.error(error),
|
|
|
+// complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
|
|
|
+// })
|
|
|
|
|
|
|
|
|
-/* Complex Test: Expected out come, both must receive 14 message by the end. Havent try to disconnect.*/
|
|
|
-setTimeout(() => {
|
|
|
- let message = {
|
|
|
- id: parsedMessages[10].appData.msgId,
|
|
|
- message: parsedMessages[10] // Choose this number, because i purposely use the 11th message and change the msgPayload property to query to emulate a request
|
|
|
- }
|
|
|
- connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
-}, 3000)
|
|
|
-setTimeout(() => {
|
|
|
- let message = {
|
|
|
- id: parsedMessages[11].appData.msgId,
|
|
|
- message: parsedMessages[11]// Choose this number, because i purposely use the 12th message and change the msgPayload property to query to emulate a request
|
|
|
- }
|
|
|
- connectionRequest.server.messageToBePublishedfromApplication.next(message)
|
|
|
-}, 4000)
|
|
|
-setTimeout(() => {
|
|
|
- console.log(`All received data: ${array.length}`)
|
|
|
-}, 10000)
|
|
|
-setTimeout(() => {
|
|
|
- console.log(`All received data: ${array.length}`)
|
|
|
-}, 20000)
|
|
|
|
|
|
|
|
|
// this is just to publish an array of fake data as a Subject
|
|
@@ -98,11 +303,11 @@ function stream(): Subject<any> {
|
|
|
const intervalId = setInterval(() => {
|
|
|
result.next(messages[count]);
|
|
|
count++;
|
|
|
- if (count >= 1000) {
|
|
|
+ if (count >= 10000) {
|
|
|
clearInterval(intervalId);
|
|
|
result.complete();
|
|
|
}
|
|
|
- }, 500)
|
|
|
+ }, intervalToStreamOutGoingMessage)
|
|
|
return result
|
|
|
}
|
|
|
|
|
@@ -122,3 +327,18 @@ function generateFakeStreamResponse(request: any): Subject<any> {
|
|
|
})
|
|
|
return res
|
|
|
}
|
|
|
+
|
|
|
+/* Checking the values by the end of the test */
|
|
|
+setTimeout(() => {
|
|
|
+ console.log(`All received data: ${array.length}`)
|
|
|
+}, 5000)
|
|
|
+setTimeout(() => {
|
|
|
+ console.log(`All received data: ${array.length}`)
|
|
|
+}, 10000)
|
|
|
+setTimeout(() => {
|
|
|
+ console.log(`All received data: ${array.length}`)
|
|
|
+}, 15000)
|
|
|
+setTimeout(() => {
|
|
|
+ console.log(`All received data: ${array.length}`)
|
|
|
+}, 20000)
|
|
|
+
|