|
| 1 | +package paypal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +// testClientID, testSecret imported from order_test.go |
| 17 | + |
| 18 | +// All test values are defined here |
| 19 | +// var testClientID = "AXy9orp-CDaHhBZ9C78QHW2BKZpACgroqo85_NIOa9mIfJ9QnSVKzY-X_rivR_fTUUr6aLjcJsj6sDur" |
| 20 | +// var testSecret = "EBoIiUSkCKeSk49hHSgTem1qnjzzJgRQHDEHvGpzlLEf_nIoJd91xu8rPOBDCdR_UYNKVxJE-UgS2iCw" |
| 21 | +var testUserID = "https://www.paypal.com/webapps/auth/identity/user/VBqgHcgZwb1PBs69ybjjXfIW86_Hr93aBvF_Rgbh2II" |
| 22 | +var testCardID = "CARD-54E6956910402550WKGRL6EA" |
| 23 | + |
| 24 | +var testProductId = "" // will be fetched in func TestProduct(t *testing.T) |
| 25 | +var testBillingPlan = "" // will be fetched in func TestSubscriptionPlans(t *testing.T) |
| 26 | + |
| 27 | +const alphabet = "abcedfghijklmnopqrstuvwxyz" |
| 28 | + |
| 29 | +func RandomString(n int) string { |
| 30 | + var sb strings.Builder |
| 31 | + k := len(alphabet) |
| 32 | + |
| 33 | + for i := 0; i < n; i++ { |
| 34 | + c := alphabet[rand.Intn(k)] |
| 35 | + sb.WriteByte(c) |
| 36 | + } |
| 37 | + return sb.String() |
| 38 | +} |
| 39 | + |
| 40 | +func createRandomProduct(t *testing.T) Product { |
| 41 | + //create a product |
| 42 | + productData := Product{ |
| 43 | + Name: RandomString(10), |
| 44 | + Description: RandomString(100), |
| 45 | + Category: ProductCategorySoftware, |
| 46 | + Type: ProductTypeService, |
| 47 | + ImageUrl: "https://example.com/image.png", |
| 48 | + HomeUrl: "https://example.com", |
| 49 | + } |
| 50 | + return productData |
| 51 | +} |
| 52 | + |
| 53 | +// this is a simple copy of the SendWithAuth method, used to |
| 54 | +// test the Lock and Unlock methods of the private mutex field |
| 55 | +// of Client structure. |
| 56 | +func (c *Client) sendWithAuth(req *http.Request, v interface{}) error { |
| 57 | + // c.Lock() |
| 58 | + c.mu.Lock() |
| 59 | + // Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)` |
| 60 | + // to happen outside of the locked section. |
| 61 | + |
| 62 | + if c.mu.TryLock() { |
| 63 | + // if the code is able to acquire a lock |
| 64 | + // despite the mutex of c being locked, throw an error |
| 65 | + err := errors.New("TryLock succeeded inside sendWithAuth with mutex locked") |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if c.Token != nil { |
| 70 | + if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn { |
| 71 | + // c.Token will be updated in GetAccessToken call |
| 72 | + if _, err := c.GetAccessToken(req.Context()); err != nil { |
| 73 | + // c.Unlock() |
| 74 | + c.mu.Unlock() |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + req.Header.Set("Authorization", "Bearer "+c.Token.Token) |
| 80 | + } |
| 81 | + // Unlock the client mutex before sending the request, this allows multiple requests |
| 82 | + // to be in progress at the same time. |
| 83 | + // c.Unlock() |
| 84 | + c.mu.Unlock() |
| 85 | + |
| 86 | + if !c.mu.TryLock() { |
| 87 | + // if the code is unable to acquire a lock |
| 88 | + // despite the mutex of c being unlocked, throw an error |
| 89 | + err := errors.New("TryLock failed inside sendWithAuth with mutex unlocked") |
| 90 | + return err |
| 91 | + } |
| 92 | + c.mu.Unlock() // undo changes from the previous TryLock |
| 93 | + |
| 94 | + return c.Send(req, v) |
| 95 | +} |
| 96 | + |
| 97 | +// this method is used to invoke the sendWithAuth method, which will then check |
| 98 | +// operationally the privated mutex field of Client structure. |
| 99 | +func (c *Client) createProduct(ctx context.Context, product Product) (*CreateProductResponse, error) { |
| 100 | + req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/catalogs/products"), product) |
| 101 | + response := &CreateProductResponse{} |
| 102 | + if err != nil { |
| 103 | + return response, err |
| 104 | + } |
| 105 | + err = c.sendWithAuth(req, response) |
| 106 | + return response, err |
| 107 | +} |
| 108 | + |
| 109 | +func TestClientMutex(t *testing.T) { |
| 110 | + c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) |
| 111 | + c.GetAccessToken(context.Background()) |
| 112 | + |
| 113 | + // Basic Testing of the private mutex field |
| 114 | + c.mu.Lock() |
| 115 | + if c.mu.TryLock() { |
| 116 | + t.Fatalf("TryLock succeeded with mutex locked") |
| 117 | + } |
| 118 | + c.mu.Unlock() |
| 119 | + if !c.mu.TryLock() { |
| 120 | + t.Fatalf("TryLock failed with mutex unlocked") |
| 121 | + } |
| 122 | + c.mu.Unlock() // undo changes from the previous TryLock |
| 123 | + |
| 124 | + // Operational testing of the private mutex field |
| 125 | + n_iter := 2 |
| 126 | + |
| 127 | + errs := make(chan error) |
| 128 | + |
| 129 | + for i := 0; i < n_iter; i++ { |
| 130 | + go func() { |
| 131 | + _, err := c.createProduct(context.Background(), createRandomProduct(t)) |
| 132 | + errs <- err |
| 133 | + }() |
| 134 | + } |
| 135 | + |
| 136 | + for i := 0; i < n_iter; i++ { |
| 137 | + err := <-errs |
| 138 | + assert.Equal(t, nil, err) |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments