Advertisement
Guest User

simplelogspec

a guest
Feb 6th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.40 KB | None | 0 0
  1. package com.treode.disk
  2.  
  3. import java.nio.file.{Paths, StandardOpenOption}
  4. import com.treode.async.{Async, Scheduler}
  5. import com.treode.async.io.File
  6. import com.treode.async.io.stubs.StubFile
  7. import com.treode.async.stubs.StubScheduler
  8. import scala.util.{Failure, Success}
  9. import com.treode.buffer.PagedBuffer
  10. import com.treode.async.Async
  11. import com.treode.async.Async.async
  12. import org.scalatest._
  13. import com.treode.async.implicits._
  14. import com.treode.async.stubs.implicits._
  15. import com.treode.async.Globals
  16.  
  17. class SimpleLog (file: StubFile) (implicit scheduler: Scheduler) {
  18.     var file_pos = 0
  19.    
  20.     def record [R] (s:String): Async [Unit] =
  21.     async { cb =>
  22.       var input = PagedBuffer (s.length())
  23.       input.writeString(s)
  24.       println("in record")
  25.       println("file pos before flush: " + file_pos)
  26.       file.flush(input, file_pos) .run {
  27.         case Success (length) => {
  28.           file_pos += s.length() + 1
  29.           println("file pos after flush: " + file_pos)
  30.          
  31.           cb.pass()
  32.         }
  33.         case Failure (thrown) => cb.fail(thrown)
  34.       }
  35.     }
  36.  }
  37.  
  38. class SimpleLogSpec extends FlatSpec {
  39.   "SimpleLog" should "record once to a StubFile" in {
  40.       implicit val scheduler = StubScheduler.random()
  41.       var testfile = StubFile(new Array[Byte](50), 0)
  42.       val rec = new SimpleLog(testfile)
  43.       var str = "hithere"
  44.       rec.record(str).expectPass()
  45.       var input = PagedBuffer(50)
  46.       testfile.fill (input, 0, str.length()) .expectPass()
  47.       assert (str == input.readString())
  48.   }
  49.  
  50.   "SimpleLog" should "record twice to a StubFile" in {
  51.     implicit val scheduler = StubScheduler.random()
  52.     var testfile = StubFile(new Array[Byte](50), 0)
  53.     val rec = new SimpleLog(testfile)
  54.     var str = "hithere"
  55.     var str2 = "nowzzz"
  56.    
  57.     //val captorOne = rec.record(str).capture()
  58.     //val captorTwo = rec.record(str2).capture()
  59.     //scheduler.run()
  60.    
  61.     rec.record(str).expectPass()
  62.     var input = PagedBuffer(50)
  63.     testfile.fill (input, 0, str.length()) .expectPass()
  64.     var actual = input.readString()
  65.     assert (str == actual)
  66.  
  67.     rec.record(str2).expectPass()
  68.     var expected = str+str2
  69.     input = PagedBuffer(50)
  70.     testfile.fill (input, 0, expected.length()) .expectPass()
  71.     actual = input.readString()
  72.     println("expected: " + expected)
  73.     println("actual: " + actual)
  74.     assert (expected == actual)
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement