Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Moved wrapper processing from item controller to processing controller
Browse files Browse the repository at this point in the history
  • Loading branch information
VladyslavAnokhin committed Jan 31, 2019
1 parent 9c76156 commit 6675f34
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 64 deletions.
6 changes: 3 additions & 3 deletions sources/VVPSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ public struct VVPSDK {
func setupVRMWithNewCore() {
let maxRedirectCount = player.model.adSettings.maxVASTWrapperRedirectCount
let prefetchOffset = player.model.adSettings.prefetchingOffset
let maxAdSearchTime: TimeInterval = 9
let maxAdSearchTime = player.model.adSettings.maxSearchTime
let createRequest: (URL) -> (URLRequest) = {
.init(url: $0, timeoutInterval: hardTimeout)
}
let adStartProcessing = StartAdProcessingController(prefetchOffset: prefetchOffset, dispatch: dispatcher)
let startGroupProcessing = StartVRMGroupProcessingController(dispatch: dispatcher)
let finishGroupProcessing = FinishVRMGroupProcessingController(dispatch: dispatcher)
let itemController = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatcher)
let itemController = VRMItemController(dispatch: dispatcher)
let itemFetchController = FetchVRMItemController(dispatch: dispatcher) { url in
self.ephemeralSession.dataFuture(with: createRequest(url))
.map(Network.Parse.successResponseData)
Expand All @@ -431,7 +431,7 @@ public struct VVPSDK {
groupsMapper: mapGroups) { url in
self.vrmProvider.requestAds(with: createRequest(url))
}
let processingController = VRMProcessingController(dispatch: dispatcher)
let processingController = VRMProcessingController(maxRedirectCount: maxRedirectCount, dispatch: dispatcher)
let createSoftTimeoutTimer = { Timer(duration: softTimeout){ dispatcher(PlayerCore.VRMCore.softTimeoutReached()) } }
let createHardTimeoutTimer = { Timer(duration: hardTimeout){ dispatcher(PlayerCore.VRMCore.hardTimeoutReached()) } }
let timeoutController = VRMTimeoutController(softTimeoutTimerFactory: createSoftTimeoutTimer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import PlayerCore

final class VRMItemController {

let maxRedirectCount: Int
let dispatch: (PlayerCore.Action) -> Void

private var startedCandidates = Set<ScheduledVRMItems.Candidate>()
private var wrapperError = Set<VRMCore.Item>()

init(maxRedirectCount: Int, dispatch: @escaping (PlayerCore.Action) -> Void ) {
self.maxRedirectCount = maxRedirectCount
init(dispatch: @escaping (PlayerCore.Action) -> Void ) {
self.dispatch = dispatch
}

Expand All @@ -29,14 +27,6 @@ final class VRMItemController {
}

scheduledItems.forEach { originalItem, queue in
guard queue.count < maxRedirectCount else {
if wrapperError.contains(originalItem) == false {
wrapperError.insert(originalItem)
dispatch(VRMCore.tooManyIndirections(item: originalItem))
}
return
}

queue.subtracting(startedCandidates)
.forEach { candidate in
startedCandidates.insert(candidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class VRMItemControllerTest: XCTestCase {
$0.url == $1.url && $0.originalItem == $1.originalItem
}

let maxRedirectCount = 3
let url = URL(string: "http://test.com")!
let vastXML = "VAST String"
var vastItem: VRMCore.Item!
Expand Down Expand Up @@ -50,7 +49,7 @@ class VRMItemControllerTest: XCTestCase {
func testMaxAdSearchTimeout() {
let dispatch = recorder.hook("testMaxAdSearchTimeout", cmp: fetchActionComparator.compare)

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)
sut = VRMItemController(dispatch: dispatch)

recorder.record {
sut.process(with: [urlItem: Set([.init(source: urlItem.source)])], isMaxAdSearchTimeReached: true)
Expand All @@ -62,7 +61,7 @@ class VRMItemControllerTest: XCTestCase {
func testStartItemsFetching() {
let dispatch = recorder.hook("compareFetchActions", cmp: fetchActionComparator.compare)

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)
sut = VRMItemController(dispatch: dispatch)

recorder.record {
sut.process(with: [urlItem: Set([.init(source: urlItem.source)])], isMaxAdSearchTimeReached: false)
Expand All @@ -76,7 +75,7 @@ class VRMItemControllerTest: XCTestCase {
func testStartItemsParsing() {
let dispatch = recorder.hook("testStartItemsParsing", cmp: parseActionComparator.compare)

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)
sut = VRMItemController(dispatch: dispatch)

recorder.record {
sut.process(with: [vastItem: Set([.init(source: vastItem.source)])], isMaxAdSearchTimeReached: false)
Expand All @@ -90,7 +89,7 @@ class VRMItemControllerTest: XCTestCase {
func testDoubleDispatch() {
let dispatch = recorder.hook("testDoubleDispatch", cmp: parseActionComparator.compare)

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)
sut = VRMItemController(dispatch: dispatch)

recorder.record {
let scheduledQueue: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>] = [vastItem: Set(arrayLiteral: .init(source: vastItem.source))]
Expand All @@ -107,7 +106,7 @@ class VRMItemControllerTest: XCTestCase {
var compare = parseActionComparator.compare
let dispatch = recorder.hook("testProcessWrapper", cmp: { compare($0,$1) })

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)
sut = VRMItemController(dispatch: dispatch)
let first = ScheduledVRMItems.Candidate(source: vastItem.source)
let second = ScheduledVRMItems.Candidate(source: .url(url))

Expand All @@ -129,36 +128,4 @@ class VRMItemControllerTest: XCTestCase {
sut.dispatch(VRMCore.startItemFetch(originalItem: vastItem, url: url))
}
}

func testMaxRedirectAndNextGroupProcess() {
var compare = wrapperErrorActionComparator.compare
let dispatch = recorder.hook("testMaxRedirect", cmp: { compare($0,$1) })

sut = VRMItemController(maxRedirectCount: maxRedirectCount, dispatch: dispatch)

let queueWithTooManyWrappers = Set<ScheduledVRMItems.Candidate>([.init(source: urlItem.source),
.init(source: .url(URL(string:"http://test1.com")!)),
.init(source: .url(URL(string:"http://test2.com")!))])
recorder.record {
sut.process(with: [urlItem: queueWithTooManyWrappers], isMaxAdSearchTimeReached: false)
sut.process(with: [urlItem: queueWithTooManyWrappers], isMaxAdSearchTimeReached: false)
}

recorder.verify {
sut.dispatch(VRMCore.tooManyIndirections(item: urlItem))
}

let correctQueue = Set<ScheduledVRMItems.Candidate>([.init(source: vastItem.source)])
compare = parseActionComparator.compare

recorder.record {
sut.process(with: [urlItem: queueWithTooManyWrappers,
vastItem: correctQueue],
isMaxAdSearchTimeReached: false)
}

recorder.verify {
sut.dispatch(VRMCore.startItemParsing(originalItem: vastItem, vastXML: vastXML))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import PlayerCore

final class VRMProcessingController {

let maxRedirectCount: Int
let dispatch: (PlayerCore.Action) -> Void

private var dispatchedResults = Set<VRMParsingResult.Result>()

init(dispatch: @escaping (PlayerCore.Action) -> Void) {
init(maxRedirectCount: Int,dispatch: @escaping (PlayerCore.Action) -> Void) {
self.maxRedirectCount = maxRedirectCount
self.dispatch = dispatch
}

func process(with state: PlayerCore.State) {
process(parsingResultQueue: state.vrmParsingResult.parsedVASTs,
scheduledVRMItems: state.vrmScheduledItems.items,
currentGroup: state.vrmCurrentGroup.currentGroup,
timeout: state.vrmProcessingTimeout,
isMaxAdSearchTimeoutReached: state.vrmMaxAdSearchTimeout.isReached)
}

func process(parsingResultQueue: [VRMCore.Item: VRMParsingResult.Result],
scheduledVRMItems: [VRMCore.Item: Set<ScheduledVRMItems.Candidate>],
currentGroup: VRMCore.Group?,
timeout: VRMProcessingTimeout,
isMaxAdSearchTimeoutReached: Bool) {
Expand All @@ -33,12 +38,23 @@ final class VRMProcessingController {
dispatchedResults.contains(result) == false
}.forEach { item, result in

if currentGroup == nil || currentGroup?.items.contains(item) == false || timeout == .hard {
if currentGroup == nil ||
currentGroup?.items.contains(item) == false ||
timeout == .hard {
dispatch(VRMCore.timeoutError(item: item))
} else if case .inline(let vast) = result.vastModel {
dispatch(VRMCore.selectInlineVAST(item: item, inlineVAST: vast))
} else if case .wrapper(let wrapper) = result.vastModel {
dispatch(VRMCore.unwrapItem(item: item, url: wrapper.tagURL))
guard let count = scheduledVRMItems[item]?.count else {
assertionFailure("try to unwrap item, which wasn't started")
return
}

if (count + 1) < maxRedirectCount {
dispatch(VRMCore.unwrapItem(item: item, url: wrapper.tagURL))
} else {
dispatch(VRMCore.tooManyIndirections(item: item))
}
}

dispatchedResults.insert(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import XCTest

class VRMProcessingControllerTest: XCTestCase {

let maxRedirectCount = 3
let recorder = Recorder()

let timeoutActionComparator = ActionComparator<VRMCore.TimeoutError> {
$0.item == $1.item
}
Expand All @@ -17,6 +19,9 @@ class VRMProcessingControllerTest: XCTestCase {
let unwrapItemActionComparator = ActionComparator<VRMCore.UnwrapItem> {
$0.item == $1.item && $0.url == $1.url
}
let wrapperErrorActionComparator = ActionComparator<VRMCore.TooManyIndirections> {
$0.item == $1.item
}

let wrapperUrl = URL(string: "http://test.com")!
var wrapper: VRMCore.VASTModel!
Expand Down Expand Up @@ -55,15 +60,18 @@ class VRMProcessingControllerTest: XCTestCase {
}

func testTimeoutItemAndDoublDispatch() {
let sut = VRMProcessingController(dispatch: recorder.hook("testTimeoutItem", cmp: timeoutActionComparator.compare))
let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount,
dispatch: recorder.hook("testTimeoutItem", cmp: timeoutActionComparator.compare))

recorder.record {
let result = VRMParsingResult.Result(vastModel: inline)
sut.process(parsingResultQueue: [vastItem: result],
scheduledVRMItems: [:],
currentGroup: nil,
timeout: .none,
isMaxAdSearchTimeoutReached: false)
sut.process(parsingResultQueue: [vastItem: result],
scheduledVRMItems: [:],
currentGroup: nil,
timeout: .none,
isMaxAdSearchTimeoutReached: false)
Expand All @@ -77,10 +85,12 @@ class VRMProcessingControllerTest: XCTestCase {
recorder.record {
let result = VRMParsingResult.Result(vastModel: inline)
sut.process(parsingResultQueue: [vastItem: result],
scheduledVRMItems: [:],
currentGroup: group,
timeout: .hard,
isMaxAdSearchTimeoutReached: false)
sut.process(parsingResultQueue: [vastItem: result],
scheduledVRMItems: [:],
currentGroup: group,
timeout: .hard,
isMaxAdSearchTimeoutReached: false)
Expand All @@ -92,11 +102,13 @@ class VRMProcessingControllerTest: XCTestCase {
}

func testItemFromAnotherGroup() {
let sut = VRMProcessingController(dispatch: recorder.hook("testItemFromAnotherGroup", cmp: timeoutActionComparator.compare))
let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount,
dispatch: recorder.hook("testItemFromAnotherGroup", cmp: timeoutActionComparator.compare))

let group = VRMCore.Group(items: [urlItem])
recorder.record {
sut.process(parsingResultQueue: [vastItem: .init(vastModel: inline)],
scheduledVRMItems: [:],
currentGroup: group,
timeout: .none,
isMaxAdSearchTimeoutReached: false)
Expand All @@ -108,12 +120,13 @@ class VRMProcessingControllerTest: XCTestCase {
}

func testSelectInlineModel() {
let sut = VRMProcessingController(dispatch: recorder.hook("testSelectInlineModel",
cmp: selectInlineAdActionComparator.compare))
let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount,
dispatch: recorder.hook("testSelectInlineModel", cmp: selectInlineAdActionComparator.compare))

let group = VRMCore.Group(items: [urlItem])
recorder.record {
sut.process(parsingResultQueue: [urlItem: .init(vastModel: inline)],
scheduledVRMItems: [:],
currentGroup: group,
timeout: .soft,
isMaxAdSearchTimeoutReached: false)
Expand All @@ -125,12 +138,12 @@ class VRMProcessingControllerTest: XCTestCase {
}

func testWrapperModel() {
let sut = VRMProcessingController(dispatch: recorder.hook("testWrapperModel",
cmp: unwrapItemActionComparator.compare))

let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount,
dispatch: recorder.hook("testWrapperModel", cmp: unwrapItemActionComparator.compare))
let group = VRMCore.Group(items: [urlItem])
recorder.record {
sut.process(parsingResultQueue: [urlItem: .init(vastModel: wrapper)],
scheduledVRMItems: [urlItem: Set()],
currentGroup: group,
timeout: .none,
isMaxAdSearchTimeoutReached: false)
Expand All @@ -142,17 +155,47 @@ class VRMProcessingControllerTest: XCTestCase {
}

func testMaxAdSearchTime() {
let sut = VRMProcessingController(dispatch: recorder.hook("testMaxAdSearchTime",
cmp: unwrapItemActionComparator.compare))
let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount,
dispatch: recorder.hook("testMaxAdSearchTime", cmp: unwrapItemActionComparator.compare))

let group = VRMCore.Group(items: [urlItem])
recorder.record {
sut.process(parsingResultQueue: [urlItem: .init(vastModel: wrapper)],
scheduledVRMItems: [:],
currentGroup: group,
timeout: .none,
isMaxAdSearchTimeoutReached: true)
}

recorder.verify {}
}

func testMaxRedirectCount() {
let queueWithTooManyWrappers = Set<ScheduledVRMItems.Candidate>([.init(source: urlItem.source),
.init(source: .url(URL(string:"http://test1.com")!))])
let parsingQueue: [VRMCore.Item: VRMParsingResult.Result] = [urlItem: .init(vastModel: wrapper)]

let hook = recorder.hook("testMaxRedirectCount", cmp: wrapperErrorActionComparator.compare)
let sut = VRMProcessingController(maxRedirectCount: maxRedirectCount, dispatch: hook)
let group = VRMCore.Group(items: [urlItem])


recorder.record {
sut.process(parsingResultQueue:parsingQueue,
scheduledVRMItems: [urlItem: queueWithTooManyWrappers],
currentGroup: group,
timeout: .none,
isMaxAdSearchTimeoutReached: false)

sut.process(parsingResultQueue: parsingQueue,
scheduledVRMItems: [urlItem: queueWithTooManyWrappers],
currentGroup: group,
timeout: .none,
isMaxAdSearchTimeoutReached: false)
}

recorder.verify {
sut.dispatch(VRMCore.tooManyIndirections(item: urlItem))
}
}
}

0 comments on commit 6675f34

Please sign in to comment.