Browse Source

Update tests, typescript declaration

v4_proto
Nicolas Dextraze 5 years ago
parent
commit
4584517ede
  1. 22
      index.d.ts
  2. 5
      src/common/utils/ensure.js
  3. 2
      src/core/eventStoreConnectionLogicHandler.js
  4. 11
      src/eventStoreNodeConnection.js
  5. 1
      src/messages/messages.js
  6. 5
      src/persistentSubscriptionSettings.js
  7. 9
      test/appendToStream_test.js
  8. 4
      test/common/base_test.js
  9. 12
      test/readEvent_test.js
  10. 15
      test/readStreamEventsBackward_test.js
  11. 13
      test/readStreamEventsForward_test.js
  12. 25
      test/subscribeToStreamFrom_test.js
  13. 3
      test/subscribeToStream_test.js
  14. 2
      test/transactions_test.js

22
index.d.ts vendored

@ -16,7 +16,7 @@ export class UserCredentials {
}
export class PersistentSubscriptionSettings {
constructor(resolveLinkTos: boolean, startFrom: number, extraStatistics: boolean, messageTimeout: number,
constructor(resolveLinkTos: boolean, startFrom: Long|number, extraStatistics: boolean, messageTimeout: number,
maxRetryCount: number, liveBufferSize: number, readBatchSize: number, historyBufferSize: number,
checkPointAfter: number, minCheckPointCount: number, maxCheckPointCount: number,
maxSubscriberCount: number, namedConsumerStrategy: string)
@ -108,7 +108,7 @@ export interface WriteResult {
export interface RecordedEvent {
readonly eventStreamId: string;
readonly eventId: string;
readonly eventNumber: number;
readonly eventNumber: Long;
readonly eventType: string;
readonly createdEpoch: number;
readonly data?: Buffer;
@ -123,17 +123,17 @@ export interface ResolvedEvent {
readonly isResolved: boolean;
readonly originalPosition?: Position;
readonly originalStreamId: string;
readonly originalEventNumber: number;
readonly originalEventNumber: Long;
}
export interface StreamEventsSlice {
readonly status: string; // TODO: enum
readonly stream: string;
readonly fromEventNumber: number;
readonly fromEventNumber: Long;
readonly readDirection: string; // TODO: enum
readonly events: ResolvedEvent[];
readonly nextEventNumber: number;
readonly lastEventNumber: number;
readonly nextEventNumber: Long;
readonly lastEventNumber: Long;
readonly isEndOfStream: boolean;
}
@ -159,7 +159,7 @@ export interface EventStoreTransaction {
export interface EventReadResult {
readonly status: string;
readonly stream: string;
readonly eventNumber: number;
readonly eventNumber: Long;
readonly event: ResolvedEvent | null;
}
@ -167,7 +167,7 @@ export interface EventStoreSubscription {
readonly isSubscribedToAll: boolean;
readonly streamId: string;
readonly lastCommitPosition: Position;
readonly lastEventNumber: number;
readonly lastEventNumber: Long;
close(): void;
unsubscribe(): void;
@ -254,9 +254,9 @@ export interface EventStoreNodeConnection {
startTransaction(stream: string, expectedVersion: Long|number, userCredentials?: UserCredentials): Promise<EventStoreTransaction>;
continueTransaction(transactionId: number, userCredentials?: UserCredentials): EventStoreTransaction;
// read actions
readEvent(stream: string, eventNumber: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<EventReadResult>;
readStreamEventsForward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
readStreamEventsBackward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
readEvent(stream: string, eventNumber: Long|number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<EventReadResult>;
readStreamEventsForward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
readStreamEventsBackward(stream: string, start: Long|number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<StreamEventsSlice>;
readAllEventsForward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
readAllEventsBackward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise<AllEventsSlice>;
// subscription actions

5
src/common/utils/ensure.js

@ -12,10 +12,11 @@ module.exports.notNull = function(value, name) {
throw new TypeError(name + " should not be null.");
};
module.exports.isInteger = function isInteger(value, name) {
function isInteger(value, name) {
if (typeof value !== 'number' || value % 1 !== 0)
throw new TypeError(name + " should be an integer.");
};
}
module.exports.isInteger = isInteger;
module.exports.isLongOrInteger = function(value, name) {
if (typeof value === 'number') {

2
src/core/eventStoreConnectionLogicHandler.js

@ -407,7 +407,7 @@ EventStoreConnectionLogicHandler.prototype._goToIdentifiedState = function() {
};
var dto = new ClientMessage.IdentifyClient({version: ClientVersion, connectionName: this._esConnection.connectionName});
var buf = dto.constructor.encode(dto).finish();
this._connection.enqueueSend(new TcpPackage(TcpCommand.IdentifyClient, this._identityInfo.correlationId, null, null, createBufferSegment(buf)))
this._connection.enqueueSend(new TcpPackage(TcpCommand.IdentifyClient, TcpFlags.None, this._identityInfo.correlationId, null, null, createBufferSegment(buf)))
};
EventStoreConnectionLogicHandler.prototype._goToConnectedState = function() {

11
src/eventStoreNodeConnection.js

@ -1,5 +1,6 @@
var util = require('util');
var uuid = require('uuid');
var Long = require('long');
var EventEmitter = require('events').EventEmitter;
var ensure = require('./common/utils/ensure');
@ -269,7 +270,7 @@ EventStoreNodeConnection.prototype.readEvent = function(stream, eventNumber, res
* Reading a specific stream forwards (async)
* @public
* @param {string} stream
* @param {number} start
* @param {Long|number} start
* @param {number} count
* @param {boolean} [resolveLinkTos]
* @param {UserCredentials} [userCredentials]
@ -279,7 +280,8 @@ EventStoreNodeConnection.prototype.readStreamEventsForward = function(
stream, start, count, resolveLinkTos, userCredentials
) {
ensure.notNullOrEmpty(stream, "stream");
ensure.isInteger(start, "start");
ensure.isLongOrInteger(start, "start");
start = Long.fromValue(start);
ensure.nonNegative(start, "start");
ensure.isInteger(count, "count");
ensure.positive(count, "count");
@ -303,7 +305,7 @@ EventStoreNodeConnection.prototype.readStreamEventsForward = function(
* Reading a specific stream backwards (async)
* @public
* @param {string} stream
* @param {number} start
* @param {Long|number} start
* @param {number} count
* @param {boolean} [resolveLinkTos]
* @param {UserCredentials} [userCredentials]
@ -313,7 +315,8 @@ EventStoreNodeConnection.prototype.readStreamEventsBackward = function(
stream, start, count, resolveLinkTos, userCredentials
) {
ensure.notNullOrEmpty(stream, "stream");
ensure.isInteger(start, "start");
ensure.isLongOrInteger(start, "start");
start = Long.fromValue(start);
ensure.isInteger(count, "count");
ensure.positive(count, "count");
if (count > MaxReadSize) throw new Error(util.format("Count should be less than %d. For larger reads you should page.", MaxReadSize));

1
src/messages/messages.js

@ -841,6 +841,7 @@
if (!(reader instanceof $Reader))
reader = $Reader.create(reader);
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.EventStore.Client.Messages.ResolvedIndexedEvent();
message.event = null;
while (reader.pos < end) {
var tag = reader.uint32();
switch (tag >>> 3) {

5
src/persistentSubscriptionSettings.js

@ -1,4 +1,6 @@
var SystemConsumerStrategies = require('./systemConsumerStrategies');
var ensure = require('./common/utils/ensure');
var Long = require('long');
function PersistentSubscriptionSettings(
resolveLinkTos, startFrom, extraStatistics, messageTimeout,
@ -6,6 +8,9 @@ function PersistentSubscriptionSettings(
checkPointAfter, minCheckPointCount, maxCheckPointCount,
maxSubscriberCount, namedConsumerStrategy
) {
ensure.isLongOrInteger(startFrom);
startFrom = Long.fromValue(startFrom);
this.resolveLinkTos = resolveLinkTos;
this.startFrom = startFrom;
this.extraStatistics = extraStatistics;

9
test/appendToStream_test.js

@ -1,5 +1,6 @@
var uuid = require('uuid');
var client = require('../src/client');
var Long = require('long');
module.exports = {
'Append One Event To Stream Happy Path': function(test) {
@ -7,7 +8,7 @@ module.exports = {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, event)
.then(function(result) {
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, 0);
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
test.ok(result.logPosition, "No log position in result.");
test.done();
})
@ -27,7 +28,7 @@ module.exports = {
}
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
.then(function(result) {
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, expectedVersion);
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
test.ok(result.logPosition, "No log position in result.");
test.done();
})
@ -47,7 +48,7 @@ module.exports = {
}
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
.then(function(result) {
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, expectedVersion);
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
test.ok(result.logPosition, "No log position in result.");
test.done();
})
@ -62,7 +63,7 @@ module.exports = {
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, event)
.then(function(result) {
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, 0);
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
test.ok(result.logPosition, "No log position in result.");
test.done();
})

4
test/common/base_test.js

@ -78,7 +78,7 @@ function testLiveEvent(name, event, evNumber) {
this.ok(event.originalPosition instanceof client.Position, name + ".originalPosition is not an instance of Position");
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
if (typeof evNumber === 'number') {
this.ok(event.originalEventNumber === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
} else {
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
}
@ -91,7 +91,7 @@ function testReadEvent(name, event, evNumber) {
this.ok(event.originalPosition === null, name + ".originalPosition is not null");
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
if (typeof evNumber === 'number') {
this.ok(event.originalEventNumber === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
} else {
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
}

12
test/readEvent_test.js

@ -1,7 +1,7 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
var Long = require('long');
module.exports = {
setUp: function(cb) {
@ -25,7 +25,7 @@ module.exports = {
.then(function(result) {
test.areEqual('status', result.status, client.eventReadStatus.Success);
test.areEqual('stream', result.stream, self.testStreamName);
test.areEqual('eventNumber', result.eventNumber, 0);
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
test.ok(result.event !== null, "event is null.");
test.ok(result.event.originalEvent !== null, "event.originalEvent is null.");
var event = JSON.parse(result.event.originalEvent.data.toString());
@ -45,7 +45,7 @@ module.exports = {
.then(function(result) {
test.areEqual('status', result.status, client.eventReadStatus.NoStream);
test.areEqual('stream', result.stream, anotherStream);
test.areEqual('eventNumber', result.eventNumber, 0);
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
test.areEqual('event', result.event, null);
test.done();
})
@ -63,7 +63,7 @@ module.exports = {
.then(function(result) {
test.areEqual('status', result.status, client.eventReadStatus.StreamDeleted);
test.areEqual('stream', result.stream, self.testStreamName);
test.areEqual('eventNumber', result.eventNumber, 0);
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(0));
test.areEqual('event', result.event, null);
test.done();
})
@ -78,7 +78,7 @@ module.exports = {
.then(function(result) {
test.areEqual('status', result.status, client.eventReadStatus.NotFound);
test.areEqual('stream', result.stream, self.testStreamName);
test.areEqual('eventNumber', result.eventNumber, 1);
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(1));
test.areEqual('event', result.event, null);
test.done();
})
@ -124,7 +124,7 @@ module.exports = {
.then(function(result) {
test.areEqual('status', result.status, client.eventReadStatus.Success);
test.areEqual('stream', result.stream, self.testStreamName);
test.areEqual('eventNumber', result.eventNumber, 1);
test.areEqual('eventNumber', result.eventNumber, Long.fromNumber(1));
test.ok(result.event !== null, "event is null.");
test.ok(result.event.originalEvent !== null, "event.originalEvent is null.");
var event = JSON.parse(result.event.originalEvent.data.toString());

15
test/readStreamEventsBackward_test.js

@ -1,6 +1,7 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
var Long = require('long');
const streamSize = 100;
@ -22,15 +23,15 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
test.areEqual('slice.readDirection', slice.readDirection, 'backward');
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, -1);
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, streamSize-1);
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, Long.fromNumber(-1));
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, Long.fromNumber(streamSize-1));
test.areEqual('slice.isEndOfStream', slice.isEndOfStream, true);
for(var i = 0; i < streamSize; i++) {
var reverseIndex = streamSize - i - 1;
test.eventEqualEventData('slice.events[' + i + ']', slice.events[i], self.eventsData[reverseIndex]);
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, reverseIndex);
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, Long.fromNumber(reverseIndex));
}
test.done();
})
@ -45,7 +46,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.sliceReadStatus.StreamNotFound);
test.areEqual('slice.stream', slice.stream, anotherStream);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})
@ -63,7 +64,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.StreamDeleted);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize-1);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})
@ -78,7 +79,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize*2);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize*2));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})

13
test/readStreamEventsForward_test.js

@ -1,6 +1,7 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
var Long = require('long');
const streamSize = 100;
@ -22,10 +23,10 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
test.areEqual('slice.readDirection', slice.readDirection, 'forward');
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, streamSize);
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, streamSize-1);
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, Long.fromNumber(streamSize));
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, Long.fromNumber(streamSize-1));
test.areEqual('slice.isEndOfStream', slice.isEndOfStream, true);
for(var i = 0; i < streamSize; i++) {
test.eventEqualEventData('slice.events[' + i + ']', slice.events[i], self.eventsData[i]);
@ -44,7 +45,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.sliceReadStatus.StreamNotFound);
test.areEqual('slice.stream', slice.stream, anotherStream);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})
@ -62,7 +63,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.StreamDeleted);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, 0);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(0));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})
@ -77,7 +78,7 @@ module.exports = {
.then(function(slice) {
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
test.areEqual('slice.stream', slice.stream, self.testStreamName);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, streamSize*2);
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize*2));
test.areEqual('slice.events.length', slice.events.length, 0);
test.done();
})

25
test/subscribeToStreamFrom_test.js

@ -22,7 +22,7 @@ function delayOnlyFirst(count, action) {
module.exports = {
'Test Subscribe to Stream From Beginning (null)': function(test) {
test.expect(36);
test.expect(32);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -57,15 +57,11 @@ module.exports = {
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.testLiveEvent('liveEvents[0]', liveEvents[0]);
test.testLiveEvent('liveEvents[1]', liveEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 2);
test.ok(liveEvents[1].originalEventNumber, 3);
test.testLiveEvent('liveEvents[0]', liveEvents[0], 2);
test.testLiveEvent('liveEvents[1]', liveEvents[1], 3);
test.ok(catchUpEvents.length === 2, "Expecting 2 catchUp event, got " + catchUpEvents.length);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0]);
test.testReadEvent('catchUpEvents[1]', catchUpEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 0);
test.ok(liveEvents[1].originalEventNumber, 1);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0], 0);
test.testReadEvent('catchUpEvents[1]', catchUpEvents[1], 1);
done(error);
}
@ -82,7 +78,7 @@ module.exports = {
.catch(test.done);
},
'Test Subscribe to Stream From 0': function(test) {
test.expect(29);
test.expect(26);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -117,13 +113,10 @@ module.exports = {
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.testLiveEvent('liveEvents[0]', liveEvents[0]);
test.testLiveEvent('liveEvents[1]', liveEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 2);
test.ok(liveEvents[1].originalEventNumber, 3);
test.testLiveEvent('liveEvents[0]', liveEvents[0], 2);
test.testLiveEvent('liveEvents[1]', liveEvents[1], 3);
test.ok(catchUpEvents.length === 1, "Expecting 1 catchUp event, got " + catchUpEvents.length);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0]);
test.ok(liveEvents[0].originalEventNumber, 1);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0], 1);
done(error);
}

3
test/subscribeToStream_test.js

@ -1,5 +1,6 @@
const uuid = require('uuid');
const client = require('../src/client');
const Long = require('long');
function delay(ms) {
return new Promise(function (resolve, reject) {
@ -58,7 +59,7 @@ module.exports = {
.then(function(subscription) {
test.areEqual("subscription.streamId", subscription.streamId, self.testStreamName);
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, false);
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, client.expectedVersion.emptyStream);
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, Long.fromNumber(client.expectedVersion.emptyStream));
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
})

2
test/transactions_test.js

@ -111,7 +111,7 @@ module.exports = {
})
.then(function(result) {
test.ok(result.logPosition, "Missing result.logPosition");
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, self.events.length-1);
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(self.events.length-1));
test.done();
})
.catch(test.done);

Loading…
Cancel
Save