summaryrefslogtreecommitdiffstats
path: root/modules/queue/unique_queue.go
blob: 87e0594ecfbf71ff74d6f537c8e2ba1e07cb3f06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package queue

import (
	"fmt"
)

// UniqueQueue defines a queue which guarantees only one instance of same
// data is in the queue. Instances with same identity will be
// discarded if there is already one in the line.
//
// This queue is particularly useful for preventing duplicated task
// of same purpose - please note that this does not guarantee that a particular
// task cannot be processed twice or more at the same time. Uniqueness is
// only guaranteed whilst the task is waiting in the queue.
//
// Users of this queue should be careful to push only the identifier of the
// data
type UniqueQueue interface {
	Queue
	PushFunc(Data, func() error) error
	Has(Data) (bool, error)
}

// ErrAlreadyInQueue is returned when trying to push data to the queue that is already in the queue
var ErrAlreadyInQueue = fmt.Errorf("already in queue")
n> Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Middleware/NotModifiedMiddlewareTest.php
blob: 58ae6b13aed9f7fd5990fe8393b51b45e021006c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\AppFramework\Middleware;

use OC\AppFramework\Middleware\NotModifiedMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\IRequest;

class NotModifiedMiddlewareTest extends \Test\TestCase {
	/** @var IRequest */
	private $request;
	/** @var Controller */
	private $controller;
	/** @var NotModifiedMiddleware */
	private $middleWare;

	protected function setUp(): void {
		parent::setUp();

		$this->request = $this->createMock(IRequest::class);
		$this->middleWare = new NotModifiedMiddleware(
			$this->request
		);

		$this->controller = $this->createMock(Controller::class);
	}

	public function dataModified(): array {
		$now = new \DateTime();

		return [
			[null, '', null, '', false],
			['etag', 'etag', null, '', false],
			['etag', '"wrongetag"', null, '', false],
			['etag', '', null, '', false],
			[null, '"etag"', null, '', false],
			['etag', '"etag"', null, '', true],

			[null, '', $now, $now->format(\DateTimeInterface::RFC2822), true],
			[null, '', $now, $now->format(\DateTimeInterface::ATOM), false],
			[null, '', null, $now->format(\DateTimeInterface::RFC2822), false],
			[null, '', $now, '', false],

			['etag', '"etag"', $now, $now->format(\DateTimeInterface::ATOM), true],
			['etag', '"etag"', $now, $now->format(\DateTimeInterface::RFC2822), true],
		];
	}

	/**
	 * @dataProvider dataModified
	 */
	public function testMiddleware(?string $etag, string $etagHeader, ?\DateTime $lastModified, string $lastModifiedHeader, bool $notModifiedSet): void {
		$this->request->method('getHeader')
			->willReturnCallback(function (string $name) use ($etagHeader, $lastModifiedHeader) {
				if ($name === 'IF_NONE_MATCH') {
					return $etagHeader;
				}
				if ($name === 'IF_MODIFIED_SINCE') {
					return $lastModifiedHeader;
				}
				return '';
			});

		$response = new Http\Response();
		if ($etag !== null) {
			$response->setETag($etag);
		}
		if ($lastModified !== null) {
			$response->setLastModified($lastModified);
		}
		$response->setStatus(Http::STATUS_OK);

		$result = $this->middleWare->afterController($this->controller, 'myfunction', $response);

		if ($notModifiedSet) {
			$this->assertSame(Http::STATUS_NOT_MODIFIED, $result->getStatus());
		} else {
			$this->assertSame(Http::STATUS_OK, $result->getStatus());
		}
	}
}