2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
2018-05-14 22:46:17 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2018, United States Government
|
2015-05-13 23:42:35 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-05-13 23:42:35 +00:00
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-05-13 23:42:35 +00:00
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2015-03-24 23:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
define(
|
|
|
|
["../src/PersistenceQueueImpl"],
|
|
|
|
function (PersistenceQueueImpl) {
|
|
|
|
|
2015-03-25 18:25:51 +00:00
|
|
|
var TEST_DELAY = 42;
|
|
|
|
|
2015-03-24 23:13:59 +00:00
|
|
|
describe("The implemented persistence queue", function () {
|
2015-03-25 18:25:51 +00:00
|
|
|
var mockQ,
|
|
|
|
mockTimeout,
|
|
|
|
mockHandler,
|
|
|
|
mockDeferred,
|
|
|
|
mockPromise,
|
|
|
|
queue;
|
|
|
|
|
|
|
|
function makeMockDomainObject(id) {
|
|
|
|
var mockDomainObject = jasmine.createSpyObj(
|
|
|
|
'domainObject-' + id,
|
2016-05-19 18:29:13 +00:00
|
|
|
['getId']
|
2015-03-25 18:25:51 +00:00
|
|
|
);
|
2018-06-30 00:32:59 +00:00
|
|
|
mockDomainObject.getId.and.returnValue(id);
|
2015-03-25 18:25:51 +00:00
|
|
|
return mockDomainObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeMockPersistence(id) {
|
|
|
|
var mockPersistence = jasmine.createSpyObj(
|
|
|
|
'persistence-' + id,
|
2016-05-19 18:29:13 +00:00
|
|
|
['persist']
|
2015-03-25 18:25:51 +00:00
|
|
|
);
|
|
|
|
return mockPersistence;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockQ = jasmine.createSpyObj('$q', ['when', 'defer']);
|
|
|
|
mockTimeout = jasmine.createSpy('$timeout');
|
|
|
|
mockHandler = jasmine.createSpyObj('handler', ['persist']);
|
|
|
|
mockDeferred = jasmine.createSpyObj('deferred', ['resolve']);
|
|
|
|
mockDeferred.promise = jasmine.createSpyObj('promise', ['then']);
|
|
|
|
mockPromise = jasmine.createSpyObj('promise', ['then']);
|
2018-06-30 00:32:59 +00:00
|
|
|
mockQ.defer.and.returnValue(mockDeferred);
|
|
|
|
mockTimeout.and.returnValue({});
|
|
|
|
mockHandler.persist.and.returnValue(mockPromise);
|
|
|
|
mockPromise.then.and.returnValue(mockPromise);
|
2015-03-25 18:25:51 +00:00
|
|
|
queue = new PersistenceQueueImpl(
|
|
|
|
mockQ,
|
|
|
|
mockTimeout,
|
|
|
|
mockHandler,
|
|
|
|
TEST_DELAY
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("schedules a timeout to persist objects", function () {
|
|
|
|
expect(mockTimeout).not.toHaveBeenCalled();
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
|
|
|
expect(mockTimeout).toHaveBeenCalledWith(
|
|
|
|
jasmine.any(Function),
|
|
|
|
TEST_DELAY,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not schedule multiple timeouts for multiple objects", function () {
|
|
|
|
// Put three objects in without triggering the timeout;
|
|
|
|
// shouldn't schedule multiple timeouts
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
|
|
|
queue.put(makeMockDomainObject('b'), makeMockPersistence('b'));
|
|
|
|
queue.put(makeMockDomainObject('c'), makeMockPersistence('c'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(1);
|
2015-03-25 18:25:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns a promise", function () {
|
|
|
|
expect(queue.put(makeMockDomainObject('a'), makeMockPersistence('a')))
|
|
|
|
.toEqual(mockDeferred.promise);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("waits for quiescence to proceed", function () {
|
|
|
|
// Keep adding objects to the queue between timeouts.
|
|
|
|
// Should keep scheduling timeouts instead of resolving.
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(1);
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
2015-03-25 18:25:51 +00:00
|
|
|
queue.put(makeMockDomainObject('b'), makeMockPersistence('b'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(2);
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
2015-03-25 18:25:51 +00:00
|
|
|
queue.put(makeMockDomainObject('c'), makeMockPersistence('c'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(3);
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
2015-03-25 18:25:51 +00:00
|
|
|
expect(mockHandler.persist).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("persists upon quiescence", function () {
|
|
|
|
// Add objects to the queue, but fire two timeouts afterward
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
|
|
|
queue.put(makeMockDomainObject('b'), makeMockPersistence('b'));
|
|
|
|
queue.put(makeMockDomainObject('c'), makeMockPersistence('c'));
|
2018-06-30 00:32:59 +00:00
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
2015-03-25 18:25:51 +00:00
|
|
|
expect(mockHandler.persist).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("waits on an active flush, while flushing", function () {
|
|
|
|
// Persist some objects
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
|
|
|
queue.put(makeMockDomainObject('b'), makeMockPersistence('b'));
|
2018-06-30 00:32:59 +00:00
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
|
|
|
expect(mockTimeout.calls.count()).toEqual(2);
|
2015-03-25 18:25:51 +00:00
|
|
|
// Adding a new object should not trigger a new timeout,
|
|
|
|
// because we haven't completed the previous flush
|
|
|
|
queue.put(makeMockDomainObject('c'), makeMockPersistence('c'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(2);
|
2015-03-25 18:25:51 +00:00
|
|
|
});
|
2015-03-24 23:13:59 +00:00
|
|
|
|
2015-03-25 18:25:51 +00:00
|
|
|
it("clears the active flush after it has completed", function () {
|
|
|
|
// Persist some objects
|
|
|
|
queue.put(makeMockDomainObject('a'), makeMockPersistence('a'));
|
|
|
|
queue.put(makeMockDomainObject('b'), makeMockPersistence('b'));
|
2018-06-30 00:32:59 +00:00
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
|
|
|
mockTimeout.calls.mostRecent().args[0]();
|
|
|
|
expect(mockTimeout.calls.count()).toEqual(2);
|
2015-03-25 18:25:51 +00:00
|
|
|
// Resolve the promise from handler.persist
|
2018-06-30 00:32:59 +00:00
|
|
|
mockPromise.then.calls.all()[0].args[0](true);
|
2015-03-25 18:25:51 +00:00
|
|
|
// Adding a new object should now trigger a new timeout,
|
|
|
|
// because we have completed the previous flush
|
|
|
|
queue.put(makeMockDomainObject('c'), makeMockPersistence('c'));
|
2018-06-30 00:32:59 +00:00
|
|
|
expect(mockTimeout.calls.count()).toEqual(3);
|
2015-03-25 18:25:51 +00:00
|
|
|
});
|
2015-03-24 23:13:59 +00:00
|
|
|
});
|
|
|
|
}
|
2016-05-19 18:29:13 +00:00
|
|
|
);
|