DefinitelyTyped/types/sc-broker/sc-broker-tests.ts
Daniel Rose 3b611ed646
Update various socketcluster types to newest versions (#42068)
* Add type definitions for async-stream-emitter, consumable-stream, writable-consumable-stream, stream-demux, ag-channel, ag-simple-broker, ncom, async-iterable-stream

* Upgrade sc-broker to 8.0

* Upgrade socketcluster-client to 15.1

* Rename definition files to match module file names

The files in the module were renamed.

* Move socketcluster-server to v14 folder

In preparation for socketcluster-server v15, since the old version is still used by other type packages.

* Update scc-broker-client to 7.0

* Add current socketcluster-server type definitions

Current version is v15.0

* Move sc-broker-cluster to v6 folder

In preparation for sc-broker-cluster v9, since the old version is still used by other type packages.

* Add current sc-broker-cluster type definitions

Current version is v9.0

* Move sc-channel to v1 folder

In preparation for sc-channel v2, since the old version is still used by other type packages.

* Add current sc-channel type definitions

Current version is v2.0

* Include the relevant sc-broker-cluster type-definitions directly in sc-channel

It can be run using older and newer version of sc-broker-cluster, which have differently versioned dependencies.

* Move sc-channel tests to sc-broker-cluster

In the tests we use sc-broker-cluster. If the tests are in sc-channel, they drag in all dependencies for sc-broker-cluster, including esnext.asynciterable, which we don't want.

* Simplify sc-errors tests

In the tests we used socketcluster-server. That dragged in all of its dependencies, including esnext.asynciterable, which we don't want.

* Move sc-channel to v1 folder

In preparation for sc-channel v2, since the old version is still used by other type packages.
2020-02-05 08:59:09 -08:00

132 lines
3.2 KiB
TypeScript

import scBroker = require('sc-broker');
import SCBroker = require('sc-broker/scbroker');
import * as scClusterBrokerClient from 'scc-broker-client';
// From the README
// $ExpectType SCBrokerServer
scBroker.createServer({ port: 9000, secretKey: 'mySecretKey' });
const conf = { port: 9000 };
const server = scBroker.createServer(conf);
server.on('ready', () => {
console.log('Server ready, create client');
const client = scBroker.createClient(conf);
// $ExpectType boolean
client.isConnected();
});
server.destroy();
const dataClient = scBroker.createClient({ port: 9000, secretKey: 'mySecretKey' });
dataClient.set(['this', 'is', 'a', 'deep', 'key'], 'Hello world');
dataClient.get(['this', 'is', 'a'], (err, val) => {
if (!err) console.log(val);
});
dataClient.add(['this', 'is', 'a'], 'foo');
dataClient.get(['this', 'is', 'a', 0], (err, val) => {
if (!err) console.log(val);
});
////////////////////////////////////////////////////
/// SCBroker tests
////////////////////////////////////////////////////
const run = () => {
console.log('run called!');
};
let broker = new SCBroker();
broker = new SCBroker({ run });
broker.options = { environment: 'prod' };
// $ExpectType number
broker.id;
// $ExpectType number
broker.instanceId;
// $ExpectType FlexiMap
broker.dataMap;
// $ExpectType ExpiryManager
broker.dataExpirer;
const subscriptions = broker.subscriptions;
// $ExpectType ComSocket
subscriptions[1]['test'];
broker
.on('subscribe', channel => {
// $ExpectType string
channel;
})
.on('unsubscribe', channel => {
// $ExpectType string
channel;
})
.on('publish', (channel, data) => {
// $ExpectType string
channel;
// $ExpectType any
data;
})
.on('masterMessage', (data, masterMessageResponse) => {
// $ExpectType any
data;
masterMessageResponse(null, 'test');
masterMessageResponse(new Error(), null);
});
broker.publish('testChannel', 123);
broker.exec(dataMap => {
dataMap.set(['main', 'message'], 'Message');
return dataMap.get(['main']);
});
broker.sendToMaster('data');
broker.sendToMaster(123, (err, response) => {
if (!err) {
// $ExpectType any
response;
}
});
class MyBroker extends SCBroker {
run() {
this.on('subscribe', channel => {});
}
}
// From the socketcluster sample
class Broker extends SCBroker {
run() {
console.log(' >> Broker PID:', process.pid);
if (this.options.clusterStateServerHost) {
scClusterBrokerClient.attach(this, {
stateServerHost: this.options.clusterStateServerHost,
stateServerPort: this.options.clusterStateServerPort,
mappingEngine: this.options.clusterMappingEngine,
clientPoolSize: this.options.clusterClientPoolSize,
authKey: this.options.clusterAuthKey,
stateServerConnectTimeout: this.options.clusterStateServerConnectTimeout,
stateServerAckTimeout: this.options.clusterStateServerAckTimeout,
stateServerReconnectRandomness: this.options.clusterStateServerReconnectRandomness,
});
}
}
}
new Broker();