-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from hyperoslo/feature/cache
Feature: cache aggregator
- Loading branch information
Showing
17 changed files
with
511 additions
and
86 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Quick | ||
import Nimble | ||
|
||
class CacheFactorySpec: QuickSpec { | ||
|
||
override func spec() { | ||
describe("CacheFactory") { | ||
|
||
describe(".register") { | ||
it("adds an item to the list of registered caches") { | ||
CacheFactory.register(.Memory, cache: MemoryCache.self) | ||
let resolvedCache = CacheFactory.resolve("Test", kind: .Memory) | ||
|
||
expect(resolvedCache is MemoryCache).to(beTrue()) | ||
} | ||
|
||
it("overrides an item in the list of registered caches") { | ||
CacheFactory.register(.Disk, cache: MemoryCache.self) | ||
let resolvedCache = CacheFactory.resolve("Test", kind: .Disk) | ||
|
||
expect(resolvedCache is MemoryCache).to(beTrue()) | ||
} | ||
} | ||
|
||
describe(".resolve") { | ||
it("returns previously registerd cache") { | ||
let kind: CacheKind = .Custom("Cloud") | ||
CacheFactory.register(kind, cache: DiskCache.self) | ||
let resolvedCache = CacheFactory.resolve("Test", kind: kind) | ||
|
||
expect(resolvedCache is DiskCache).to(beTrue()) | ||
} | ||
|
||
it("returns default registered caches") { | ||
CacheFactory.reset() | ||
let memoryCache = CacheFactory.resolve("Test", kind: .Memory) | ||
let diskCache = CacheFactory.resolve("Test", kind: .Disk, maxSize: 0) | ||
|
||
expect(memoryCache is MemoryCache).to(beTrue()) | ||
expect(diskCache is DiskCache).to(beTrue()) | ||
} | ||
|
||
it("returns memory cache for unresolved kind") { | ||
CacheFactory.reset() | ||
let resolvedCache = CacheFactory.resolve("Test", kind: .Custom("Weirdo")) | ||
|
||
expect(resolvedCache is MemoryCache).to(beTrue()) | ||
} | ||
|
||
it("returns a cache with specified maxSize") { | ||
let resolvedCache = CacheFactory.resolve("Test", kind: .Memory, maxSize: 1000) | ||
expect(resolvedCache.maxSize).to(equal(1000)) | ||
} | ||
} | ||
|
||
describe(".reset") { | ||
it("resets to defaults") { | ||
CacheFactory.register(.Disk, cache: MemoryCache.self) | ||
CacheFactory.reset() | ||
let resolvedCache = CacheFactory.resolve("Test", kind: .Disk) | ||
|
||
expect(resolvedCache is DiskCache).to(beTrue()) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import Quick | ||
import Nimble | ||
|
||
class CacheSpec: QuickSpec { | ||
|
||
override func spec() { | ||
describe("Cache") { | ||
let name = "WeirdoCache" | ||
let key = "alongweirdkey" | ||
let object = User(firstName: "John", lastName: "Snow") | ||
var cache: Cache<User>! | ||
|
||
beforeEach { | ||
cache = Cache<User>(name: name) | ||
} | ||
|
||
afterEach { | ||
cache.clear() | ||
} | ||
|
||
describe("#init") { | ||
|
||
it("sets a name") { | ||
expect(cache.name).to(equal(name)) | ||
} | ||
|
||
it("sets a config") { | ||
let defaultConfig = Config.defaultConfig | ||
|
||
expect(cache.config.frontKind.name).to(equal(defaultConfig.frontKind.name)) | ||
expect(cache.config.backKind!.name).to(equal(defaultConfig.backKind!.name)) | ||
expect(cache.config.expiry.date).to(equal(defaultConfig.expiry.date)) | ||
expect(cache.config.maxSize).to(equal(defaultConfig.maxSize)) | ||
} | ||
|
||
it("sets the front cache as a memory cache") { | ||
expect(cache.frontCache.self is MemoryCache).to(beTrue()) | ||
expect(cache.backCache).toNot(beNil()) | ||
expect(cache.backCache!.self is DiskCache).to(beTrue()) | ||
} | ||
} | ||
|
||
describe("#add") { | ||
it("saves an object to memory and disk") { | ||
let expectation1 = self.expectationWithDescription("Save Expectation") | ||
let expectation2 = self.expectationWithDescription("Save To Memory Expectation") | ||
let expectation3 = self.expectationWithDescription("Save To Disk Expectation") | ||
|
||
cache.add(key, object: object) { | ||
cache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).toNot(beNil()) | ||
expectation1.fulfill() | ||
} | ||
|
||
cache.frontCache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).toNot(beNil()) | ||
expectation2.fulfill() | ||
} | ||
|
||
cache.backCache?.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).toNot(beNil()) | ||
expectation3.fulfill() | ||
} | ||
} | ||
|
||
self.waitForExpectationsWithTimeout(8.0, handler:nil) | ||
} | ||
} | ||
|
||
describe("#object") { | ||
it("resolves cached object") { | ||
let expectation = self.expectationWithDescription("Object Expectation") | ||
|
||
cache.add(key, object: object) { | ||
cache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject?.firstName).to(equal(object.firstName)) | ||
expect(receivedObject?.lastName).to(equal(object.lastName)) | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
self.waitForExpectationsWithTimeout(4.0, handler:nil) | ||
} | ||
} | ||
|
||
describe("#remove") { | ||
it("removes cached object from memory and disk") { | ||
let expectation1 = self.expectationWithDescription("Remove Expectation") | ||
let expectation2 = self.expectationWithDescription("Remove From Memory Expectation") | ||
let expectation3 = self.expectationWithDescription("Remove From Disk Expectation") | ||
|
||
cache.add(key, object: object) | ||
|
||
cache.remove(key) { | ||
cache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation1.fulfill() | ||
} | ||
|
||
cache.frontCache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation2.fulfill() | ||
} | ||
|
||
cache.backCache?.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation3.fulfill() | ||
} | ||
} | ||
|
||
self.waitForExpectationsWithTimeout(8.0, handler:nil) | ||
} | ||
} | ||
|
||
describe("#clear") { | ||
it("clears memory and disk cache") { | ||
let expectation1 = self.expectationWithDescription("Clear Expectation") | ||
let expectation2 = self.expectationWithDescription("Clear Memory Expectation") | ||
let expectation3 = self.expectationWithDescription("Clear Disk Expectation") | ||
|
||
cache.add(key, object: object) | ||
|
||
cache.clear() { | ||
cache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation1.fulfill() | ||
} | ||
|
||
cache.frontCache.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation2.fulfill() | ||
} | ||
|
||
cache.backCache?.object(key) { (receivedObject: User?) in | ||
expect(receivedObject).to(beNil()) | ||
expectation3.fulfill() | ||
} | ||
} | ||
|
||
self.waitForExpectationsWithTimeout(8.0, handler:nil) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Quick | ||
import Nimble | ||
|
||
class ConfigSpec: QuickSpec { | ||
|
||
override func spec() { | ||
describe("Config") { | ||
|
||
describe(".defaultConfig") { | ||
it("returns the correct default config") { | ||
let config = Config.defaultConfig | ||
|
||
expect(config.frontKind.name).to(equal(CacheKind.Memory.name)) | ||
expect(config.backKind!.name).to(equal(CacheKind.Disk.name)) | ||
expect(config.expiry.date).to(equal(Expiry.Never.date)) | ||
expect(config.maxSize).to(equal(0)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Quick | ||
import Nimble | ||
|
||
class CacheKindSpec: QuickSpec { | ||
|
||
override func spec() { | ||
describe("CacheKind") { | ||
|
||
describe("#name") { | ||
it("returns the correct name for default values") { | ||
expect(CacheKind.Memory.name).to(equal("Memory")) | ||
expect(CacheKind.Disk.name).to(equal("Disk")) | ||
} | ||
|
||
it("returns the correct name for custom values") { | ||
expect(CacheKind.Custom("Weirdo").name).to(equal("Weirdo")) | ||
} | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.