Skip to content
Snippets Groups Projects
Commit 23eaf0e1 authored by Aaron Davidson's avatar Aaron Davidson
Browse files

[SPARK-4264] Completion iterator should only invoke callback once

Author: Aaron Davidson <aaron@databricks.com>

Closes #3128 from aarondav/compiter and squashes the following commits:

698e4be [Aaron Davidson] [SPARK-4264] Completion iterator should only invoke callback once
parent b41a39e2
No related branches found
No related tags found
No related merge requests found
......@@ -25,10 +25,13 @@ private[spark]
// scalastyle:off
abstract class CompletionIterator[ +A, +I <: Iterator[A]](sub: I) extends Iterator[A] {
// scalastyle:on
private[this] var completed = false
def next() = sub.next()
def hasNext = {
val r = sub.hasNext
if (!r) {
if (!r && !completed) {
completed = true
completion()
}
r
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.util
import org.scalatest.FunSuite
class CompletionIteratorSuite extends FunSuite {
test("basic test") {
var numTimesCompleted = 0
val iter = List(1, 2, 3).iterator
val completionIter = CompletionIterator[Int, Iterator[Int]](iter, { numTimesCompleted += 1 })
assert(completionIter.hasNext)
assert(completionIter.next() === 1)
assert(numTimesCompleted === 0)
assert(completionIter.hasNext)
assert(completionIter.next() === 2)
assert(numTimesCompleted === 0)
assert(completionIter.hasNext)
assert(completionIter.next() === 3)
assert(numTimesCompleted === 0)
assert(!completionIter.hasNext)
assert(numTimesCompleted === 1)
// SPARK-4264: Calling hasNext should not trigger the completion callback again.
assert(!completionIter.hasNext)
assert(numTimesCompleted === 1)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment