Skip to content

Commit

Permalink
#3 thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 8, 2019
1 parent 4508aa0 commit 07f8abe
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 23 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ SOFTWARE.
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.25.3</version>
<version>0.42</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<version>0.17.2</version>
<version>0.22.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
Expand All @@ -105,6 +105,7 @@ SOFTWARE.
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-log</artifactId>
<version>0.18.1</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/yegor256/rpm/Pkg.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.nio.file.Path;
import java.util.logging.Level;
import org.cactoos.Scalar;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;
import org.redline_rpm.ReadableChannelWrapper;
import org.redline_rpm.Scanner;
import org.redline_rpm.header.AbstractHeader;
Expand All @@ -56,16 +56,16 @@ final class Pkg {
/**
* The header.
*/
private final UncheckedScalar<Header> hdr;
private final Unchecked<Header> hdr;

/**
* Ctor.
* @param path The path
*/
Pkg(final Path path) {
this.file = path;
this.hdr = new UncheckedScalar<>(
new StickyScalar<>(
this.hdr = new Unchecked<>(
new Sticky<>(
new Scalar<Header>() {
@Override
public Header value() throws Exception {
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/com/yegor256/rpm/Rpm.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,24 @@ public Rpm(final Storage stg) {
* @throws IOException If fails
*/
public void update(final String key) throws IOException {
final Path temp = Files.createTempFile("rpm", ".rpm");
this.storage.load(key, temp);
final Pkg pkg = new Pkg(temp);
final Repomd repomd = new Repomd(this.storage);
repomd.update(
"primary",
file -> new Primary(file).update(key, pkg)
);
repomd.update(
"filelists",
file -> new Filelists(file).update(pkg)
);
repomd.update(
"other",
file -> new Other(file).update(pkg)
);
synchronized (this.storage) {
final Path temp = Files.createTempFile("rpm", ".rpm");
this.storage.load(key, temp);
final Pkg pkg = new Pkg(temp);
final Repomd repomd = new Repomd(this.storage);
repomd.update(
"primary",
file -> new Primary(file).update(key, pkg)
);
repomd.update(
"filelists",
file -> new Filelists(file).update(pkg)
);
repomd.update(
"other",
file -> new Other(file).update(pkg)
);
}
}

}
102 changes: 102 additions & 0 deletions src/test/java/com/yegor256/rpm/RpmTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.yegor256.rpm;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XMLDocument;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Repeated;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link Rpm}.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.0.3
*/
public final class RpmTest {

/**
* Temp folder for all tests.
*/
@Rule
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public TemporaryFolder folder = new TemporaryFolder();

/**
* Rpm storage works, in many threads.
* @throws Exception If some problem inside
*/
@Test
@SuppressWarnings("unchecked")
public void addsSingleRpm() throws Exception {
final Storage storage = new Storage.Simple(
this.folder.newFolder().toPath()
);
final Path bin = this.folder.newFile("x.rpm").toPath();
final String key = "nginx-module-xslt-1.16.1-1.el7.ngx.x86_64.rpm";
Files.copy(
RpmITCase.class.getResourceAsStream(
String.format("/%s", key)
),
bin,
StandardCopyOption.REPLACE_EXISTING
);
storage.save(key, bin);
final Rpm rpm = new Rpm(storage);
final int threads = 10;
MatcherAssert.assertThat(
new Threads<>(
threads,
new Repeated<Scalar<Boolean>>(
threads,
() -> {
rpm.update(key);
return true;
}
)
),
Matchers.iterableWithSize(threads)
);
final Path primary = this.folder.newFile("primary.xml").toPath();
storage.load("repodata/primary.xml", primary);
MatcherAssert.assertThat(
new XMLDocument(new String(Files.readAllBytes(primary))),
XhtmlMatchers.hasXPath(
"/ns1:metadata/ns1:package[ns1:name='nginx-module-xslt']",
"http://linux.duke.edu/metadata/common"
)
);
}

}

0 comments on commit 07f8abe

Please sign in to comment.