Skip to content

Commit

Permalink
[SPARK-4264] Completion iterator should only invoke callback once
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondav committed Nov 6, 2014
1 parent cb0eae3 commit 698e4be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 comments on commit 698e4be

Please sign in to comment.