Skip to content

Commit

Permalink
[MOB-14325] - Adding few more essential API to ThreadSafePrimitives (#…
Browse files Browse the repository at this point in the history
…629)

* Making the Webview of fullscreen message public

* [MOB-14325] - Adding more essential API to ThreadSafePrimitives

* [AMSDK-14325] - Fix warnings

* [AMSDK-14325] - Better unittest

* [MOB-14325] - run swift format
  • Loading branch information
PravinPK authored May 19, 2021
1 parent b5be88e commit ccf4bb6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion AEPServices/Sources/utility/ThreadSafeArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class ThreadSafeArray<T> {
queue = DispatchQueue(label: identifier)
}

/// Appends a new element safetly to the array
/// Appends a new element to the thread safe array
public func append(_ newElement: T) {
queue.async {
self.array.append(newElement)
Expand All @@ -36,6 +36,17 @@ public final class ThreadSafeArray<T> {
}
}

/// Removes and returns the first element of the thread safe array.
/// Returns nil if the array is empty
public func removeFirst() -> T? {
queue.sync {
if array.isEmpty {
return nil
}
return self.array.removeFirst()
}
}

/// Returns if the array is empty or not
public var isEmpty: Bool {
return queue.sync { return self.array.isEmpty }
Expand Down
5 changes: 5 additions & 0 deletions AEPServices/Sources/utility/ThreadSafeDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public final class ThreadSafeDictionary<K: Hashable, V> {
return queue.sync { return self.dictionary.keys.count }
}

/// A collection containing just the keys of the dictionary.
public var keys: [K] {
return queue.sync { return Array(self.dictionary.keys) }
}

// Gets a non-thread-safe shallow copy of the backing dictionary
public var shallowCopy: [K: V] {
return queue.sync {
Expand Down
23 changes: 23 additions & 0 deletions AEPServices/Tests/utility/ThreadSafeArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,29 @@ class ThreadSafeArrayTests: XCTestCase {
XCTAssertEqual(tsArray.count, 0)
}


// Tests that removes first item from an array
func testRemoveFirst() {
let tsArray = ThreadSafeArray<String>()

// test removeFirst on empty array
XCTAssertNil(tsArray.removeFirst())

// setup
tsArray.append("One")
tsArray.append("Two")

// test
XCTAssertEqual(tsArray.removeFirst(), "One")
XCTAssertEqual(tsArray.count, 1)

// test
XCTAssertEqual(tsArray.removeFirst(), "Two")
XCTAssertEqual(tsArray.count, 0)


}

// Tests that the .shallowCopy functionality doesn't deadlock against the backing array
func testShallowCopyNoDeadlock() {
let count = 1000
Expand Down
14 changes: 14 additions & 0 deletions AEPServices/Tests/utility/ThreadSafeDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ class ThreadSafeDictionaryTests: XCTestCase {
}
}

func testGetKeys() {
let count = 100
let testDictionary = ThreadSafeDictionary<Int, Int>()

// get keys on empty dictionary
XCTAssertEqual(testDictionary.keys,[])

for i in 0 ..< count {
testDictionary[i] = i
}

XCTAssertEqual(testDictionary.keys.count, count)
}

private func dispatchSyncWithDict(i: Int) {
dispatchQueueSerial.sync {
self.threadSafeDict?[0] = i
Expand Down

0 comments on commit ccf4bb6

Please sign in to comment.