Skip to content

Commit

Permalink
test: add refresh tests for manual flush
Browse files Browse the repository at this point in the history
  • Loading branch information
pwrightcertinia committed Dec 12, 2024
1 parent 148c3f0 commit b0c646e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
8 changes: 4 additions & 4 deletions jvm/src/main/scala/com/nawforce/apexlink/org/Flusher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import com.nawforce.pkgforce.path.PathLike
import scala.collection.mutable

trait RefreshListener {
def onRefresh(orgPath: PathLike, updatedPath: PathLike): Unit
def onRefreshAll(orgPath: PathLike, updatedPaths: Seq[PathLike]): Unit
def onRefreshOne(orgPath: PathLike, updatedPath: PathLike): Unit
def onRefreshMany(orgPath: PathLike, updatedPaths: Seq[PathLike]): Unit
}

case class RefreshRequest(pkg: OPM.PackageImpl, path: PathLike, highPriority: Boolean)
Expand All @@ -47,7 +47,7 @@ class Flusher(org: OPM.OrgImpl, parsedCache: Option[ParsedCache]) {
org.refreshLock.synchronized {
val updated = request.pkg.refreshBatched(Seq(request))
// Notify of updated path
if (updated) listener.foreach(_.onRefresh(org.path, request.path))
if (updated) listener.foreach(_.onRefreshOne(org.path, request.path))

// Tell auto flush we skipped the queue
skippedQueue |= updated
Expand Down Expand Up @@ -84,7 +84,7 @@ class Flusher(org: OPM.OrgImpl, parsedCache: Option[ParsedCache]) {
flush()

// Notify of updated paths
if (updated) listener.foreach(_.onRefreshAll(org.path, updatedPaths.toSeq))
if (updated) listener.foreach(_.onRefreshMany(org.path, updatedPaths.toSeq))

updated
}
Expand Down
2 changes: 1 addition & 1 deletion jvm/src/test/scala/com/nawforce/apexlink/TestHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package com.nawforce.apexlink

import com.nawforce.apexlink.TestHelper.{CURSOR, locToString}
import com.nawforce.apexlink.api._
import com.nawforce.apexlink.org.{OPM, OrgInfo}
import com.nawforce.apexlink.org.{OPM, OrgInfo, RefreshListener}
import com.nawforce.apexlink.plugins.{Plugin, PluginsManager}
import com.nawforce.apexlink.rpc.{LocationLink, TargetLocation}
import com.nawforce.apexlink.types.apex.{ApexClassDeclaration, ApexFullDeclaration, FullDeclaration}
Expand Down
59 changes: 58 additions & 1 deletion jvm/src/test/scala/com/nawforce/apexlink/pkg/RefreshTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
package com.nawforce.apexlink.pkg

import com.nawforce.apexlink.TestHelper
import com.nawforce.apexlink.org.OPM
import com.nawforce.apexlink.org.{OPM, RefreshListener}
import com.nawforce.pkgforce.PathInterpolator.PathInterpolator
import com.nawforce.pkgforce.names.{Name, Names, TypeName}
import com.nawforce.pkgforce.path.PathLike
import com.nawforce.runtime.FileSystemHelper
import org.scalatest.funsuite.AnyFunSuite

import scala.collection.mutable

class RefreshTest extends AnyFunSuite with TestHelper {

private def refresh(
Expand All @@ -33,6 +35,17 @@ class RefreshTest extends AnyFunSuite with TestHelper {
pkg.refresh(path, highPriority)
}

private def refreshAll(pkg: OPM.PackageImpl, paths: Map[PathLike, String]): Unit = {
paths.foreach(p => p._1.write(p._2))
pkg.refreshAll(paths.keys.toArray)
}

class Listener(val capture: mutable.ArrayBuffer[PathLike]) extends RefreshListener {
def onRefreshOne(orgPath: PathLike, updatedPath: PathLike): Unit = capture.addOne(updatedPath)
def onRefreshMany(orgPath: PathLike, updatedPaths: Seq[PathLike]): Unit =
capture.addAll(updatedPaths)
}

test("Valid refresh") {
withManualFlush {
FileSystemHelper.run(Map("pkg/Foo.cls" -> "public class Foo {}")) { root: PathLike =>
Expand Down Expand Up @@ -1158,4 +1171,48 @@ class RefreshTest extends AnyFunSuite with TestHelper {
}
}

test("Refresh listener with refresh") {
val capture = mutable.ArrayBuffer[PathLike]()

withManualFlush {
FileSystemHelper.run(Map("pkg/Foo.cls" -> "public class Foo {}")) { root: PathLike =>
val org = createOrg(root)
org.setRefreshListener(Some(new Listener(capture)))
val pkg = org.unmanaged
refresh(pkg, root.join("pkg/Foo.cls"), "public class Foo {}")
assert(org.flush())
assert(org.issues.isEmpty)
assert(capture.map(_.toString).toSeq.sorted.equals(Seq("/pkg/Foo.cls")))
}
}
}

test("Refresh listener with refreshAll") {
val capture = mutable.ArrayBuffer[PathLike]()

withManualFlush {
FileSystemHelper.run(
Map(
"pkg/Foo.cls" -> "public class Foo {}",
"pkg/Bar.cls" -> "public class Bar {}",
"pkg/Baz.cls" -> "public class Baz {}"
)
) { root: PathLike =>
val org = createOrg(root)
org.setRefreshListener(Some(new Listener(capture)))
val pkg = org.unmanaged
refreshAll(
pkg,
Map(
root.join("pkg/Foo.cls") -> "public class Foo {}",
root.join("pkg/Bar.cls") -> "public class Bar {}"
)
)
assert(org.flush())
assert(org.issues.isEmpty)
assert(capture.map(_.toString).toSeq.sorted.equals(Seq("/pkg/Bar.cls", "/pkg/Foo.cls")))
}
}
}

}

0 comments on commit b0c646e

Please sign in to comment.