Skip to content

Commit 6b26526

Browse files
authored
Merge pull request #184 from BranchMetrics/buo-cache-unit-tests
BUO cache unit tests
2 parents 44e16b8 + 7a7f6d5 commit 6b26526

File tree

6 files changed

+204
-19
lines changed

6 files changed

+204
-19
lines changed

native-tests/android/app/src/test/java/io/branch/nativetests/AgingHashUnitTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,53 @@
1212

1313
public class AgingHashUnitTest {
1414
@Test
15-
public void testTtl() {
15+
public void testTtlInitialization() {
1616
AgingHash<String, String> hash = new AgingHash<>(3600000);
17+
18+
// getTtlMillis() returns the constructor argument.
1719
assertEquals(3600000, hash.getTtlMillis());
1820
}
21+
22+
@Test
23+
public void testGetReturnsNullWhenNotFound() {
24+
AgingHash<String, String> hash = new AgingHash<>(3600000);
25+
26+
// get() returns null when the key is not present.
27+
assertNull(hash.get("key"));
28+
}
29+
30+
@Test
31+
public void testPutAndGet() {
32+
AgingHash<String, String> hash = new AgingHash<>(3600000);
33+
hash.put("key", "value");
34+
35+
// get() returns a value if the key is present and has not expired.
36+
assertEquals(hash.get("key"), "value");
37+
}
38+
39+
@Test
40+
public void testRemove() {
41+
AgingHash<String, String> hash = new AgingHash<>(3600000);
42+
hash.put("key", "value");
43+
hash.remove("key");
44+
45+
// get() returns null after remove() is called with the same key.
46+
assertNull(hash.get("key"));
47+
}
48+
49+
@Test
50+
public void testExpiration() {
51+
AgingHash<String, String> hash = new AgingHash<>(1);
52+
hash.put("key", "value");
53+
54+
try {
55+
Thread.sleep(10);
56+
} catch(Exception e) {
57+
fail("Failed to sleep for 10 ms: " + e.getMessage());
58+
}
59+
60+
// Deletes expired keys on insertion
61+
hash.put("newKey", "newValue");
62+
assertNull(hash.get("key"));
63+
}
1964
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.branch.nativetests;
2+
3+
import org.junit.Test;
4+
5+
import io.branch.rnbranch.AgingItem;
6+
7+
import static org.junit.Assert.*;
8+
9+
/**
10+
* Created by jdee on 4/24/17.
11+
*/
12+
13+
public class AgingItemUnitTest {
14+
@Test
15+
public void testInitialization() {
16+
String value = "abc";
17+
18+
long before = System.currentTimeMillis();
19+
AgingItem<String> item = new AgingItem<>(value);
20+
long after = System.currentTimeMillis();
21+
22+
// Access time initialized to initialization time.
23+
assertTrue(item.getAccessTime() >= before);
24+
assertTrue(item.getAccessTime() <= after);
25+
26+
// get() returns the constructor argument.
27+
assertEquals(value, item.get());
28+
}
29+
30+
@Test
31+
public void testAccessTime() {
32+
String value = "abc";
33+
34+
AgingItem<String> item = new AgingItem<>(value);
35+
long afterInitialization = System.currentTimeMillis();
36+
37+
try {
38+
Thread.sleep(10);
39+
}
40+
catch (Exception e) {
41+
fail("Failed to sleep for 10 ms: " + e.getMessage());
42+
}
43+
44+
item.get();
45+
long afterAccess = System.currentTimeMillis();
46+
47+
// Access time updated after calling get()
48+
assertTrue(item.getAccessTime() >= afterInitialization);
49+
assertTrue(item.getAccessTime() <= afterAccess);
50+
}
51+
}

native-tests/android/app/src/test/java/io/branch/nativetests/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

native-tests/ios/NativeTests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7B1D14B71EAE62A500A0BC26 /* RNBranchAgingItemTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B1D14B61EAE62A500A0BC26 /* RNBranchAgingItemTests.m */; };
1011
7B23B6671E9FDCEA006CEE75 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B23B6661E9FDCEA006CEE75 /* main.m */; };
1112
7B7C90E31EA6E07C005B470E /* NSObjectExtensionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B7C90E21EA6E07C005B470E /* NSObjectExtensionTests.m */; };
1213
7B7C90E51EA6E496005B470E /* BranchUniversalObjectExtensionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B7C90E41EA6E496005B470E /* BranchUniversalObjectExtensionTests.m */; };
@@ -32,6 +33,7 @@
3233
/* Begin PBXFileReference section */
3334
392B02218804C2D13A35994D /* Pods-NativeTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NativeTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-NativeTests/Pods-NativeTests.release.xcconfig"; sourceTree = "<group>"; };
3435
525D0BB38E8DFEDF9864FC5F /* Pods-NativeTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NativeTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-NativeTests/Pods-NativeTests.debug.xcconfig"; sourceTree = "<group>"; };
36+
7B1D14B61EAE62A500A0BC26 /* RNBranchAgingItemTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNBranchAgingItemTests.m; sourceTree = "<group>"; };
3537
7B23B6621E9FDCEA006CEE75 /* NativeTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NativeTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
3638
7B23B6661E9FDCEA006CEE75 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
3739
7B23B6761E9FDCEB006CEE75 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -128,6 +130,7 @@
128130
7B7C90E41EA6E496005B470E /* BranchUniversalObjectExtensionTests.m */,
129131
7B7C90E21EA6E07C005B470E /* NSObjectExtensionTests.m */,
130132
7BCEC8AF1E9FE59A00271263 /* RNBranchAgingDictionaryTests.m */,
133+
7B1D14B61EAE62A500A0BC26 /* RNBranchAgingItemTests.m */,
131134
7B7C90E61EA6E71E005B470E /* RNBranchPropertyTests.m */,
132135
7BAD46FA1EAA66E700080008 /* RNBranchTests.m */,
133136
7B23B6811E9FDCEB006CEE75 /* Info.plist */,
@@ -355,6 +358,7 @@
355358
files = (
356359
7B7C90E71EA6E71E005B470E /* RNBranchPropertyTests.m in Sources */,
357360
7B7C90E91EA6EC8C005B470E /* BranchLinkPropertiesExtensionTests.m in Sources */,
361+
7B1D14B71EAE62A500A0BC26 /* RNBranchAgingItemTests.m in Sources */,
358362
7BCEC8B01E9FE59A00271263 /* RNBranchAgingDictionaryTests.m in Sources */,
359363
7B7C90E31EA6E07C005B470E /* NSObjectExtensionTests.m in Sources */,
360364
7B7C90E51EA6E496005B470E /* BranchUniversalObjectExtensionTests.m in Sources */,

native-tests/ios/NativeTestsTests/RNBranchAgingDictionaryTests.m

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,60 @@ @interface RNBranchAgingDictionaryTests : XCTestCase
1515

1616
@implementation RNBranchAgingDictionaryTests
1717

18-
- (void)testTtl {
18+
- (void)testTtlInitialization {
1919
RNBranchAgingDictionary *dictionary = [RNBranchAgingDictionary dictionaryWithTtl:3600.0];
20+
21+
// ttl property returns the constructor argument.
2022
XCTAssertEqual(3600.0, dictionary.ttl);
2123
}
2224

25+
- (void)testValueIsNullWhenKeyNotFound
26+
{
27+
RNBranchAgingDictionary *dictionary = [RNBranchAgingDictionary dictionaryWithTtl:3600.0];
28+
29+
// key access returns nil when the key is not present.
30+
XCTAssertNil(dictionary[@"key"]);
31+
XCTAssertNil([dictionary objectForKey:@"key"]);
32+
}
33+
34+
- (void)testInsertionAndRetrieval
35+
{
36+
RNBranchAgingDictionary *dictionary = [RNBranchAgingDictionary dictionaryWithTtl:3600.0];
37+
38+
dictionary[@"key1"] = @"value1";
39+
[dictionary setObject:@"value2" forKey:@"key2"];
40+
41+
// access returns a value if the key is present and has not expired.
42+
XCTAssertEqual(@"value1", dictionary[@"key1"]);
43+
XCTAssertEqual(@"value1", [dictionary objectForKey:@"key1"]);
44+
45+
XCTAssertEqual(@"value2", dictionary[@"key2"]);
46+
XCTAssertEqual(@"value2", [dictionary objectForKey:@"key2"]);
47+
}
48+
49+
- (void)testRemoval
50+
{
51+
RNBranchAgingDictionary *dictionary = [RNBranchAgingDictionary dictionaryWithTtl:3600.0];
52+
53+
dictionary[@"key"] = @"value";
54+
[dictionary removeObjectForKey:@"key"];
55+
56+
// access returns nil after a key is removed.
57+
XCTAssertNil(dictionary[@"key"]);
58+
}
59+
60+
- (void)testExpiration
61+
{
62+
RNBranchAgingDictionary *dictionary = [RNBranchAgingDictionary dictionaryWithTtl:1e-3]; // 1 ms TTL
63+
64+
dictionary[@"key1"] = @"value1";
65+
66+
usleep(10000); // sleep for 10 ms
67+
68+
dictionary[@"key2"] = @"value2";
69+
70+
// Deletes expired keys on insertion
71+
XCTAssertNil(dictionary[@"key1"]);
72+
}
73+
2374
@end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// RNBranchAgingItemTests.m
3+
// NativeTests
4+
//
5+
// Created by Jimmy Dee on 4/24/17.
6+
// Copyright © 2017 Branch Metrics. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
11+
#import <react-native-branch/RNBranchAgingItem.h>
12+
13+
@interface RNBranchAgingItemTests : XCTestCase
14+
15+
@end
16+
17+
@implementation RNBranchAgingItemTests
18+
19+
- (void)testInitialization
20+
{
21+
NSTimeInterval beforeInitialization = [NSDate date].timeIntervalSince1970;
22+
RNBranchAgingItem *item = [[RNBranchAgingItem alloc] initWithItem:@"abc"];
23+
NSTimeInterval afterInitialization = [NSDate date].timeIntervalSince1970;
24+
25+
// Access time initialized to initialization time.
26+
XCTAssertGreaterThanOrEqual(item.accessTime, beforeInitialization);
27+
XCTAssertLessThanOrEqual(item.accessTime, afterInitialization);
28+
29+
// item property returns the constructor argument.
30+
XCTAssertEqual(@"abc", item.item);
31+
}
32+
33+
- (void)testAccessTime
34+
{
35+
RNBranchAgingItem *item = [[RNBranchAgingItem alloc] initWithItem:@"abc"];
36+
NSTimeInterval afterInitialization = [NSDate date].timeIntervalSince1970;
37+
38+
usleep(10000); // sleep for 10 ms
39+
40+
NSString *value = item.item;
41+
NSTimeInterval afterAccess = [NSDate date].timeIntervalSince1970;
42+
43+
// Access time updated after calling [item item].
44+
XCTAssertGreaterThanOrEqual(item.accessTime, afterInitialization);
45+
XCTAssertLessThanOrEqual(item.accessTime, afterAccess);
46+
47+
// Avoid a complaint about an unused variable.
48+
XCTAssertEqual(@"abc", value);
49+
}
50+
51+
@end

0 commit comments

Comments
 (0)