Skip to content

Commit

Permalink
Refactor create file function
Browse files Browse the repository at this point in the history
  • Loading branch information
SwikritiT committed Nov 29, 2021
1 parent 171e4b6 commit 4000a82
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions tests/acceptance/helpers/webdavHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,44 +192,30 @@ exports.createFolder = function (user, folderName) {
* @param {string} user
* @param {string} fileName
* @param {string} contents
* @param {number} waitMaxIfExisting
*/
exports.createFile = async function (user, fileName, contents = '', waitMaxIfExisting = 10000) {
exports.createFile = async function (user, fileName, contents = '') {
const davPath = exports.createDavPath(user, fileName)
/**
* makes sure upload operations are carried out maximum once a second to avoid version issues
* see https://github.com/owncloud/core/issues/23151
*/
uploadTimeStamps[user] = uploadTimeStamps[user] || {}

const pollCheck = async (retries = 0, waitFor = 100, waitMax = waitMaxIfExisting) => {
if (!uploadTimeStamps[user][fileName] || waitMax <= waitFor) {
return
} else {
uploadTimeStamps[user][fileName] = true
if (uploadTimeStamps[user] && uploadTimeStamps[user][fileName]) {
const timeSinceLastFileUpload = Date.now() - uploadTimeStamps[user][fileName]
if (timeSinceLastFileUpload <= 2001) {
await client.pause(2001 - timeSinceLastFileUpload)
}

retries++
await client.pause(waitFor)

// O(n)
await pollCheck(retries, waitFor * retries, waitMax)
}

await pollCheck()

const davPath = exports.createDavPath(user, fileName)
const putResponse = await httpHelper.put(davPath, user, contents)

delete uploadTimeStamps[user][fileName]

const statusResponse = await httpHelper.checkStatus(
putResponse,
`Could not create the file "${fileName}" for user "${user}".`
)

await client.pause(500)

return statusResponse.text()
return httpHelper
.put(davPath, user, contents)
.then(function (res) {
uploadTimeStamps[user] = uploadTimeStamps[user] || {}
uploadTimeStamps[user][fileName] = Date.now()
return httpHelper.checkStatus(
res,
`Could not create the file "${fileName}" for user "${user}".`
)
})
.then((res) => res.text())
}

/**
Expand Down

0 comments on commit 4000a82

Please sign in to comment.