pongo_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package engine
  2. import (
  3. "html/template"
  4. "net/http/httptest"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/xyproto/algernon/lua/pool"
  9. "github.com/xyproto/algernon/utils"
  10. "github.com/xyproto/datablock"
  11. )
  12. func pongoPageTest(n int, t *testing.T) {
  13. req := httptest.NewRequest("GET", "/", nil)
  14. w := httptest.NewRecorder()
  15. filename := "testdata/index.po2"
  16. luafilename := "testdata/data.lua"
  17. pongodata, err := os.ReadFile(filename)
  18. if err != nil {
  19. t.Fatalf("Failed reading file: %s", err)
  20. }
  21. ac, err := New("Algernon 123", "Just a test")
  22. if err != nil {
  23. t.Fatalf("Failed creating new Algernon instance: %s", err)
  24. }
  25. // Use a FileStat cache with different settings
  26. ac.SetFileStatCache(datablock.NewFileStat(true, time.Minute*1))
  27. ac.cache = datablock.NewFileCache(20000000, true, 64*utils.KiB, true, 0)
  28. luablock, err := ac.cache.Read(luafilename, ac.shouldCache(".po2"))
  29. if err != nil {
  30. t.Fatalf("Failed reading Lua file from cache: %s", err)
  31. }
  32. // luablock can be empty if there was an error or if the file was empty
  33. if !luablock.HasData() {
  34. t.Fatal("Lua block does not have data")
  35. }
  36. // Lua LState pool
  37. ac.luapool = pool.New()
  38. defer ac.luapool.Shutdown()
  39. // Make functions from the given Lua data available
  40. errChan := make(chan error)
  41. funcMapChan := make(chan template.FuncMap)
  42. go ac.Lua2funcMap(w, req, filename, luafilename, ".lua", errChan, funcMapChan)
  43. funcs := <-funcMapChan
  44. err = <-errChan
  45. if err != nil {
  46. t.Fatalf("Error with Lua2funcMap: %s", err)
  47. }
  48. // Trigger the error (now resolved)
  49. for i := 0; i < n; i++ {
  50. go ac.PongoPage(w, req, filename, pongodata, funcs)
  51. }
  52. }
  53. func TestPongoPage(t *testing.T) {
  54. pongoPageTest(1, t)
  55. }
  56. //func TestConcurrentPongoPage1(t *testing.T) {
  57. // pongoPageTest(10, t)
  58. //}
  59. //
  60. //func TestConcurrentPongoPage2(t *testing.T) {
  61. // for i := 0; i < 10; i++ {
  62. // go pongoPageTest(1, t)
  63. // }
  64. //}
  65. //
  66. //func TestConcurrentPongoPage3(t *testing.T) {
  67. // for i := 0; i < 10; i++ {
  68. // go pongoPageTest(10, t)
  69. // }
  70. //}
  71. //
  72. //func TestConcurrentPongoPage4(t *testing.T) {
  73. // for i := 0; i < 1000; i++ {
  74. // go pongoPageTest(1000, t)
  75. // }
  76. //}