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.

81 lines
2.2 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. const 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. proofServerClient = NewProofServerClient(apiURL, pollInterval)
  19. err := proofServerClient.WaitReady(context.Background())
  20. if err != nil {
  21. panic(err)
  22. }
  23. exitVal = m.Run()
  24. }
  25. os.Exit(exitVal)
  26. }
  27. func TestApiServer(t *testing.T) {
  28. t.Run("testAPIStatus", testAPIStatus)
  29. t.Run("testCalculateProof", testCalculateProof)
  30. time.Sleep(time.Second / 4)
  31. err := proofServerClient.WaitReady(context.Background())
  32. require.NoError(t, err)
  33. t.Run("testGetProof", testGetProof)
  34. t.Run("testCancel", testCancel)
  35. }
  36. func testAPIStatus(t *testing.T) {
  37. status, err := proofServerClient.apiStatus(context.Background())
  38. require.NoError(t, err)
  39. assert.Equal(t, true, status.Status.IsReady())
  40. }
  41. func testCalculateProof(t *testing.T) {
  42. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  43. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  44. require.NoError(t, err)
  45. }
  46. func testGetProof(t *testing.T) {
  47. proof, _, err := proofServerClient.GetProof(context.Background())
  48. require.NoError(t, err)
  49. require.NotNil(t, proof)
  50. require.NotNil(t, proof.PiA)
  51. require.NotNil(t, proof.PiB)
  52. require.NotNil(t, proof.PiC)
  53. require.NotNil(t, proof.Protocol)
  54. }
  55. func testCancel(t *testing.T) {
  56. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  57. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  58. require.NoError(t, err)
  59. // TODO: remove sleep when the server has been reviewed
  60. time.Sleep(time.Second / 4)
  61. err = proofServerClient.Cancel(context.Background())
  62. require.NoError(t, err)
  63. status, err := proofServerClient.apiStatus(context.Background())
  64. require.NoError(t, err)
  65. for status.Status == StatusCodeBusy {
  66. time.Sleep(proofServerClient.pollInterval)
  67. status, err = proofServerClient.apiStatus(context.Background())
  68. require.NoError(t, err)
  69. }
  70. assert.Equal(t, StatusCodeAborted, status.Status)
  71. }