# Tests where a request is a non-active waiter.

new-lock-table maxlocks=10000
----

new-txn txn=txn1 ts=10 epoch=0
----

new-txn txn=txn2 ts=10 epoch=0
----

new-request r=req1 txn=txn1 ts=10 spans=intent@a+none@b+intent@c
----

scan r=req1
----
start-waiting: false

add-discovered r=req1 k=a txn=txn2
----
num=1
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

add-discovered r=req1 k=b txn=txn2
----
num=2
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]

add-discovered r=req1 k=c txn=txn2
----
num=3
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

# req1 is not in the queue for "b" as readers are never inactive waiters.

print
----
num=3
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

new-request r=req2 txn=txn1 ts=10 spans=intent@c
----

scan r=req2
----
start-waiting: true

# req2 is the distinguished waiter at "c".

print
----
num=3
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

scan r=req1
----
start-waiting: true

guard-state r=req1
----
new: state=waitFor txn=txn2 key="a" held=true guard-strength=Intent

print
----
num=3
 lock: "a"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: true req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

# req1 waits at "c" but not as distinguished waiter.
release txn=txn2 span=a
----
num=3
 lock: "a"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

guard-state r=req1
----
new: state=waitFor txn=txn2 key="c" held=true guard-strength=Intent

print
----
num=3
 lock: "a"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   queued locking requests:
    active: true req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

# req1 waits at "b" as reader.

release txn=txn2 span=c
----
num=3
 lock: "a"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
 lock: "c"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

guard-state r=req1
----
new: state=waitFor txn=txn2 key="b" held=true guard-strength=None

guard-state r=req2
----
new: state=waitSelf

print
----
num=3
 lock: "a"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "b"
  holder: txn: 00000000-0000-0000-0000-000000000002 epoch: 0, iso: Serializable, ts: 10.000000000,0, info: repl [Intent]
   waiting readers:
    req: 1, txn: 00000000-0000-0000-0000-000000000001
 lock: "c"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

# req1 is done waiting.

release txn=txn2 span=b
----
num=2
 lock: "a"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
 lock: "c"
   queued locking requests:
    active: false req: 1, strength: Intent, txn: 00000000-0000-0000-0000-000000000001
    active: true req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

guard-state r=req1
----
new: state=doneWaiting

dequeue r=req1
----
num=1
 lock: "c"
   queued locking requests:
    active: false req: 2, strength: Intent, txn: 00000000-0000-0000-0000-000000000001

guard-state r=req2
----
new: state=doneWaiting

dequeue r=req2
----
num=0
