You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5 KiB

  1. package prover
  2. import (
  3. "context"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. var apiURL = "http://localhost:3000/api"
  13. const pollInterval = 1 * time.Second
  14. var proofServerClient *ProofServerClient
  15. func TestMain(m *testing.M) {
  16. exitVal := 0
  17. if os.Getenv("INTEGRATION") != "" {
  18. _apiURL := os.Getenv("PROOF_SERVER_URL")
  19. if _apiURL != "" {
  20. apiURL = _apiURL
  21. }
  22. proofServerClient = NewProofServerClient(apiURL, pollInterval)
  23. err := proofServerClient.WaitReady(context.Background())
  24. if err != nil {
  25. panic(err)
  26. }
  27. exitVal = m.Run()
  28. }
  29. os.Exit(exitVal)
  30. }
  31. func TestApiServer(t *testing.T) {
  32. t.Run("testAPIStatus", testAPIStatus)
  33. t.Run("testCalculateProof", testCalculateProof)
  34. time.Sleep(time.Second / 4)
  35. err := proofServerClient.WaitReady(context.Background())
  36. require.NoError(t, err)
  37. t.Run("testGetProof", testGetProof)
  38. t.Run("testCancel", testCancel)
  39. }
  40. func testAPIStatus(t *testing.T) {
  41. status, err := proofServerClient.apiStatus(context.Background())
  42. require.NoError(t, err)
  43. assert.Equal(t, true, status.Status.IsReady())
  44. }
  45. func testCalculateProof(t *testing.T) {
  46. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  47. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  48. require.NoError(t, err)
  49. }
  50. func testGetProof(t *testing.T) {
  51. proof, pubInputs, err := proofServerClient.GetProof(context.Background())
  52. require.NoError(t, err)
  53. assert.NotNil(t, proof.PiA)
  54. assert.NotEqual(t, [2]*big.Int{}, proof.PiA)
  55. assert.NotNil(t, proof.PiB)
  56. assert.NotEqual(t, [3][2]*big.Int{}, proof.PiB)
  57. assert.NotNil(t, proof.PiC)
  58. assert.NotEqual(t, [2]*big.Int{}, proof.PiC)
  59. assert.NotNil(t, proof.Protocol)
  60. assert.NotEqual(t, 0, len(pubInputs))
  61. }
  62. func testCancel(t *testing.T) {
  63. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  64. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  65. require.NoError(t, err)
  66. // TODO: remove sleep when the server has been reviewed
  67. time.Sleep(time.Second / 4)
  68. err = proofServerClient.Cancel(context.Background())
  69. require.NoError(t, err)
  70. status, err := proofServerClient.apiStatus(context.Background())
  71. require.NoError(t, err)
  72. for status.Status == StatusCodeBusy {
  73. time.Sleep(proofServerClient.pollInterval)
  74. status, err = proofServerClient.apiStatus(context.Background())
  75. require.NoError(t, err)
  76. }
  77. assert.Equal(t, StatusCodeAborted, status.Status)
  78. }