-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Тесты для воспроизведения ошибки работы с многопоточностью
- Loading branch information
1 parent
3035cd6
commit 6c65b18
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
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,63 @@ | ||
// | ||
// Test_Threadsafety.swift | ||
// EasyDi-iOS-Tests | ||
// | ||
// Created by Sergey V. Krupov on 21.11.2018. | ||
// Copyright © 2018 AndreyZarembo. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import EasyDi | ||
|
||
fileprivate protocol SomeProtocol { | ||
} | ||
|
||
fileprivate class SomeObject: SomeProtocol { | ||
var values = Array<String>(repeating: "", count: 4000) | ||
} | ||
|
||
fileprivate class TestAssembly: Assembly { | ||
|
||
var someObject: SomeProtocol { | ||
return define(init: SomeObject()) { | ||
for i in 0 ..< $0.values.count { | ||
$0.values[i] = self.getSomeValue(at: i) | ||
} | ||
return $0 | ||
} | ||
} | ||
|
||
// Сделано для того, чтобы стабильно воспроизводить падение. Вряд ли в реальном приложении будет такой код. | ||
private func getSomeValue(at index: Int) -> String { | ||
return define(key: "getSomeValue_\(index)", init: "value-\(index)") | ||
} | ||
} | ||
|
||
final class Test_Threadsafety: XCTestCase { | ||
|
||
func test_ThreadSafety() { | ||
|
||
let context = DIContext() | ||
let assembly = TestAssembly.instance(from: context) | ||
|
||
// Явно создаю 2 потока, т.к. не известно на скольких потоках будет работать concurrent dispatch queue | ||
|
||
let expectation1 = expectation(description: "Thread-1") | ||
Thread.detachNewThread { | ||
for _ in 1 ..< 10 { | ||
_ = assembly.someObject | ||
} | ||
expectation1.fulfill() | ||
} | ||
|
||
let expectation2 = expectation(description: "Thread-2") | ||
Thread.detachNewThread { | ||
for _ in 1 ..< 10 { | ||
_ = assembly.someObject | ||
} | ||
expectation2.fulfill() | ||
} | ||
|
||
wait(for: [expectation1, expectation2], timeout: 10) | ||
} | ||
} |